如何在终端中执行“python”命令,运行 python3 而不是 python2?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23048756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make the "python" command in terminal, run python3 instead of python2?
提问by Charlotteis
I'm just starting to learn Python and did search around a little, so forgive me if this has been asked and answered.
我刚刚开始学习 Python 并进行了一些搜索,所以如果有人问过并回答过这个问题,请原谅我。
When running scripts through the command line/terminal, I have to type "python3" to run the latest version of Python. With Python 2.X I just use "python".
通过命令行/终端运行脚本时,我必须键入“python3”才能运行最新版本的 Python。使用 Python 2.XI 只需使用“python”。
Is there a way to run Python 3 just using "python"? It may seem a little lazy, but I'm mostly just curious if it is possible or if it will break anything unnecessarily if I could in fact do it.
有没有办法只使用“python”来运行 Python 3?它可能看起来有点懒惰,但我主要只是好奇它是否可能,或者如果我实际上可以做到的话,它是否会破坏任何不必要的东西。
采纳答案by Ffisegydd
If you're using Windows then you can use the Python Launcher For Windows.
如果您使用的是 Windows,那么您可以使用Python Launcher For Windows。
This will allow you to use the py
command to select different python installations such as:
这将允许您使用该py
命令来选择不同的 python 安装,例如:
py -2.7 # Runs Python 2.7
py -3.3 # Runs Python 3.3
py -2 # Runs the latest version of Python 2.x (so if you have 2.6 and 2.7 it will run 2.7)
Similarly you can set a shebang in your python files as demonstrated below:
同样,您可以在您的 python 文件中设置一个shebang,如下所示:
#! python3
print('Hello World!')
If you now run that file (let's call it test.py
) with py test.py
it will automatically run with Python 3. It gets the Python installation to use from the shebang at the beginning of the line.
如果您现在运行该文件(我们称之为test.py
),py test.py
它将自动与 Python 3 一起运行。它从该行开头的 shebang 获取要使用的 Python 安装。
What you probably want is to customise the default python versionthough. This will allow you to set the default actions if you just call py
on it's own.
不过,您可能想要的是自定义默认的 Python 版本。如果您只是py
自行调用,这将允许您设置默认操作。
回答by CraicerHyman
Sounds like you have python 2 and 3 installed and your pythonpath is pointed at python 2, so unless specified it uses that version. If you are using python I would suggest setting up a virtual environment (virtualenv) for each project, which means you could run whatever version you'd like in that project and keep all dependencies contained.
听起来你已经安装了 python 2 和 3 并且你的 pythonpath 指向 python 2,所以除非指定它使用那个版本。如果您使用的是 python,我建议为每个项目设置一个虚拟环境 (virtualenv),这意味着您可以在该项目中运行您想要的任何版本并保留所有依赖项。
回答by Dan Walsh
If you are using Linux, add the following into into ~/.bashrc
alias python=python3
Restart the shell and type python and python3 should start instead of python2.
如果您使用的是 Linux,请将以下内容添加到 ~/.bashrc 中
alias python=python3
重新启动 shell 并键入 python 和 python3 应启动而不是 python2。
回答by Lokesh Meher
According to PEP-394,
"for the time being, all distributions should ensure that python refers to the same target as python2
".
On *nix systems, there are three links to executables of python command line interpreter named
python
, python2
and python3
in directory /usr/bin
. The python
link points to python2
according to the PEP, but you can change it to point to python3
by creating a new link to python3
and renaming it to python
. Also, you have to delete the old python
link.
根据PEP-394,
“ for the time being, all distributions should ensure that python refers to the same target as python2
”。
在 *nix 系统上,有三个指向名为 的 python 命令行解释器的可执行文件的链接
python
,python2
并且python3
在目录/usr/bin
. 该python
链接python2
根据 PEP指向,但您可以python3
通过创建新链接python3
并将其重命名为python
. 此外,您必须删除旧python
链接。
回答by james
on raspbian linux in the terminal i just run it by typing python3 file.py or just python file.py for python 2
在终端的 raspbian linux 上,我只是通过键入 python3 file.py 或 python 2 的 python file.py 来运行它
回答by Dimang Chou
Once you installed python 3 in your Mac, "python3" command will be registered into the environment variable automatically. So if you need to run your python 3 file just do that:
在 Mac 中安装 python 3 后,“python3”命令将自动注册到环境变量中。因此,如果您需要运行 python 3 文件,请执行以下操作:
python3 your_file_name.py
I hope this help you.
我希望这对你有帮助。