How to choose which version of python runs from terminal?

依然范特西╮ 提交于 2020-11-30 04:40:39

问题


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?


回答1:


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

$ 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 python2 replaced by the version you want) to ~/.bashrc in linux or ~/.bash_profile in Mac. (You'll need to restart your terminal session in this case.)




回答2:


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

$ 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.

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

Also see:

  • Comprehensive beginner's virtualenv tutorial?
  • Use different Python version with virtualenv



回答3:


py -3 or py -2 etc to choose between versions. See https://docs.python.org/3/installing/#work-with-multiple-versions-of-python-installed-in-parallel




回答4:


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

For example:

alias python='python2.7'

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




回答5:


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



来源:https://stackoverflow.com/questions/22739382/how-to-choose-which-version-of-python-runs-from-terminal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!