如何选择从终端运行哪个版本的python?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22739382/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:32:46  来源:igfitidea点击:

How to choose which version of python runs from terminal?

pythonunix

提问by mjmostachetti

I have a few different versions of python on my computer. How do I choose which one is run from my terminal when I type "python" into the prompt?

我的电脑上有几个不同版本的 python。当我在提示中输入“python”时,如何选择从终端运行哪一个?

回答by alecxe

You should have multiple executables for every python version you have. For example, if I type pythonand hit tab, I see:

对于您拥有的每个 Python 版本,您都应该有多个可执行文件。例如,如果我输入python并点击选项卡,我会看到:

$ python
python             python2.5-config   python2.7-config   python3.3          python3.3m-config  pythonw2.7         pythonw3.3-32      
python-config      python2.6          python3            python3.3-32       pythonw            pythonw3           
python2            python2.6-config   python3-32         python3.3-config   pythonw2.5         pythonw3-32        
python2.5          python2.7          python3-config     python3.3m         pythonw2.6         pythonw3.3 

So, if, for example, I want python 2.5 version - I run python2.5.

因此,例如,如果我想要 python 2.5 版本 - 我运行python2.5.

Also, take a look at virtual environments- it's much easier to manage and switch between multiple python environments with it.

另外,看看虚拟环境- 使用它管理和切换多个 python 环境要容易得多。

Also see:

另见:

回答by wkunker

To choose which version of python is run when you type 'python' into a terminal, you may want to try using an alias.

要在终端中键入“python”时选择运行哪个版本的 python,您可能需要尝试使用别名。

For example:

例如:

alias python='python2.7'

Would make python2.7 execute when you type 'python' into your terminal.

当您在终端中键入“python”时,将使 python2.7 执行。

回答by Pi Marillion

Use whichto see where your python command resides. Then use ls -lto find out where it reallyis. Then link the one you want instead. Note that the other installed versions are usually all available by their respective names.

使用which,看看你的Python命令驻留。然后用它ls -l来找出它的真正位置。然后链接你想要的那个。请注意,其他已安装的版本通常都以其各自的名称提供。

$ which python
/usr/bin/python
$ ls -l /usr/bin/python
lrwxrwxrwx 1 root root 9 Jun 18  2013 /usr/bin/python -> python2.7
$ ls /usr/bin/python*
/usr/bin/python   /usr/bin/python2.7         /usr/bin/python2-config
/usr/bin/python2  /usr/bin/python2.7-config  /usr/bin/python-config
$ sudo ln -sf /usr/bin/python2 /usr/bin/python

Note that this changes which Python version all programs for all users on your computer will probably use! If you only want to change it for yourself. You can alias it by adding a alias python='/usr/bin/python2'line (with python2replaced by the version you want) to ~/.bashrcin linux or ~/.bash_profilein Mac. (You'll need to restart your terminal session in this case.)

请注意,这会更改您计算机上所有用户的所有程序可能使用的Python 版本!如果你只想为自己改变它。您可以通过在 linux 或Mac 中添加alias python='/usr/bin/python2'一行(python2替换为您想要的版本)来为其~/.bashrc命名~/.bash_profile。(在这种情况下,您需要重新启动终端会话。)

回答by shorrty

Try envirius (universal virtual environments manager), which allows to compile any version of python. Moreover, it allows to create environments with mixed languages.

试试envirius(通用虚拟环境管理器),它允许编译任何版本的python。此外,它允许创建混合语言的环境。