如何在终端中切换 Python 版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43354382/
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 to switch Python versions in Terminal?
提问by heapoverflow
My Mac came with Python 2.7 installed by default, but I'd like to use Python 3.6.1 instead.
我的 Mac 默认安装了 Python 2.7,但我想改用 Python 3.6.1。
How can I change the Python version used in Terminal (on Mac OS)?
如何更改终端(在 Mac OS 上)中使用的 Python 版本?
Please explain clearly and offer no third party version manager suggestions.
请解释清楚,不要提供第三方版本管理器建议。
回答by Inian
The simplest way would be to add an alias to python3to always point to the native pythoninstalled. Add this line to the .bash_profilefile in your $HOMEdirectory at the last,
最简单的方法是添加一个别名python3以始终指向python安装的本机。最后将此行添加到.bash_profile您$HOME目录中的文件中,
alias python="python3"
Doing so makes the changes to be reflected on every interactive shell opened.
这样做会使更改反映在每个打开的交互式 shell 上。
回答by Ajax1234
You can just specify the python version when running a program:
您可以在运行程序时指定 python 版本:
for python 2:
对于蟒蛇 2:
python filename.py
for python 3:
对于蟒蛇 3:
python3 filename.py
回答by Scheme
As Inian suggested, you should alias python to point to python 3. It is very easy to do, and very easy to switchback, personally i have an alias setup for p2=python2 and p3=python3 as well to save on keystrokes. Read here for more information: How do I create a Bash alias?
正如 Inian 建议的那样,您应该将 python 别名为指向 python 3。这很容易做到,也很容易切换,我个人也为 p2=python2 和 p3=python3 设置了别名,以节省击键次数。阅读此处了解更多信息:如何创建 Bash 别名?
Here is an example of doing so for python:
这是一个为 python 执行此操作的示例:
alias python=python3
Like so:
像这样:
$ python --version
Python 2.7.6
$ python3 --version
Python 3.4.3
$ alias python=python3
$ python --version
Python 3.4.3
See here for the original: https://askubuntu.com/questions/320996/how-to-make-python-program-command-execute-python-3
原文见这里:https: //askubuntu.com/questions/320996/how-to-make-python-program-command-execute-python-3
回答by Fadil Olamyy Wahab
If you have python various versions of python installed,you can launch any of them using pythonx.x.xwhere x.x.xrepresents your versions.
如果你安装了 python 的各种版本的 python,你可以使用代表你的版本的pythonx.x.xwhere启动它们中的任何一个x.x.x。
回答by Skeptic
Here is a nice and simple way to do it (but on CENTOS), without braking the operating system.
这是一个很好且简单的方法(但在 CENTOS 上),而无需制动操作系统。
yum install scl-utils
next
下一个
yum install centos-release-scl-rh
And lastly you install the version that you want, lets say python3.5
最后你安装你想要的版本,比如 python3.5
yum install rh-python35
And lastly:
最后:
scl enable rh-python35 bash
Since MAC-OS is a unix operating system, the way to do it it should be quite similar.
由于 MAC-OS 是一个 unix 操作系统,所以它的实现方式应该非常相似。
回答by Martin Thoma
pyenvis a 3rd party version managerwhich is super commonly used (18k stars, 1.6k forks) and exactly what I looked for when I came to this question.
pyenv是一个超级常用的3rd 方版本管理器(18k 星,1.6k 分叉),这正是我遇到这个问题时所寻找的。
Install pyenv.
安装pyenv.
Usage
用法
$ pyenv install --list
Available versions:
2.1.3
[...]
3.8.1
3.9-dev
activepython-2.7.14
activepython-3.5.4
activepython-3.6.0
anaconda-1.4.0
[... a lot more; including anaconda, miniconda, activepython, ironpython, pypy, stackless, ....]
$ pyenv install 3.8.1
Downloading Python-3.8.1.tar.xz...
-> https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tar.xz
Installing Python-3.8.1...
Installed Python-3.8.1 to /home/moose/.pyenv/versions/3.8.1
$ pyenv versions
* system (set by /home/moose/.pyenv/version)
2.7.16
3.5.7
3.6.9
3.7.4
3.8-dev
$ python --version
Python 2.7.17
$ pip --version
pip 19.3.1 from /home/moose/.local/lib/python3.6/site-packages/pip (python 3.6)
$ mkdir pyenv-experiment && echo "3.8.1" > "pyenv-experiment/.python-version"
$ cd pyenv-experiment
$ python --version
Python 3.8.1
$ pip --version
pip 19.2.3 from /home/moose/.pyenv/versions/3.8.1/lib/python3.8/site-packages/pip (python 3.8)

