【问题标题】:set default python version when run the program as executable ./xxx.py - on linux将程序作为可执行文件运行时设置默认python版本./xxx.py - 在Linux上
【发布时间】:2015-08-13 23:47:17
【问题描述】:

我想将 python 直接作为可执行文件运行,例如./xxx.py

但是,当我这样运行它时,它会自动使用 python 2.7 而不是 python 3。

如何使用 python 3 运行可执行的 python 文件?

情况:

./magane.py shell

它会给我

Python 2.7.9 (default, Apr  2 2015, 15:33:21) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> 

但我想要这个:

python3 manage.py shell
/home/cliu/.local/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py:57: RuntimeWarning: SQLite received a naive datetime (2015-05-31 15:24:46.785297) while time zone support is active.
  RuntimeWarning)

Python 3.4.3 (default, Mar 26 2015, 22:03:40) 
Type "copyright", "credits" or "license" for more information.

IPython 2.3.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: 

python3 使用 ipython 的地方。

【问题讨论】:

    标签: python linux python-2.7 ubuntu python-3.x


    【解决方案1】:

    要告诉 python 使用哪个版本并使其在 Linux 上可执行,您必须执行以下步骤:

    在 python 脚本中添加#!

    #!/usr/bin/env python3
    

    添加运行权限

    chmod +x xxx.py
    

    【讨论】:

      【解决方案2】:

      作为我们脚本的第一行,您可以使用:

      #!/usr/bin/python3
      

      通过这种方式,您可以将此行添加到编写为在 Python3 上运行的脚本中,否则忽略。

      【讨论】:

        猜你喜欢
        • 2015-02-23
        • 2019-05-19
        • 2018-04-02
        • 2020-11-22
        • 1970-01-01
        • 2017-11-27
        • 1970-01-01
        • 2018-01-14
        • 1970-01-01
        相关资源
        最近更新 更多