美文网首页
[Python环境搭建]Python解释器

[Python环境搭建]Python解释器

作者: mHubery | 来源:发表于2019-03-15 20:50 被阅读0次

    使用HomeBrew安装Python 3,会自动添加软链接: /usr/local/bin/python3/usr/local/bin本身就在PATH路径上。这样我们可以直接在命令行输入命令python3

    $ ls -l /usr/local/bin/python3
    lrwxr-xr-x  1 root  wheel  69 Sep 30 10:14 /usr/local/bin/python3 -> ../../../Library/Frameworks/Python.framework/Versions/3.7/bin/python3
    $ echo $PATH
    /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin 
    

    在Unix系统上,Python 3.x解释器默认不安装名为python的可执行文件,因此它不会与同时安装的Python 2.x可执行文件冲突。

    Python 3有两种打开方式:

    1. Python交互模式
    2. python可执行文件

    Python交互模式


    在命令行模式下敲命令python3,进入Python交互模式。提示符为>>>

    $ python3
    Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) 
    [Clang 6.0 (clang-600.0.57)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print("Hello, World!")
    Hello, World!
    >>> 
    

    在Python交互模式下敲命令exit(),退出Python交互模式

    >>> exit()
    $ 
    

    python可执行文件


    python可执行文件后缀名是.py
    在命令行模式下敲命令python3 file,即可运行python文件

    hello.py文件
    ------------
    print("Hello, World!")
    ------------
    $ python hello.py
    Hello, World!
    

    也可将hello.py文件“改造”为可执行文件后,直接执行

    hello.py文件
    ------------
    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    print("Hello, World!")
    ------------
    $ chmod a+x hello.py
    $ ./hello.py
    Hello, World!
    

    hello.py文件第一行为UNIX “shebang”语句
    第二行指明文件编码方式,默认编码模式也是utf-8

    相关文章

      网友评论

          本文标题:[Python环境搭建]Python解释器

          本文链接:https://www.haomeiwen.com/subject/kfxdmqtx.html