gpt4 book ai didi

python - 将程序作为可执行文件运行时设置默认 python 版本 ./xxx.py - 在 linux 上

转载 作者:太空狗 更新时间:2023-10-29 11:32:05 25 4
gpt4 key购买 nike

我想直接将 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脚本中添加#!

#!/usr/bin/env python3

添加运行它的权限

chmod +x xxx.py

关于python - 将程序作为可执行文件运行时设置默认 python 版本 ./xxx.py - 在 linux 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30557888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com