简体   繁体   中英

Switching between python2 and python3 as the default python

Is there a standard way to switch between python2 to python3 as the default python, similar to how virtualenv can be used to switch between different sandboxed python environments?

I would like to avoid manually fiddling with symlinks and the PATH variable so that the solution is portable.

Since it is about switching python version, the solution would preferably not be written in python but rather in bash or something portable.

Ideally I would like to find something something similar to nvm for nodejs or rbenv for Ruby.

向我们展示了如何使用update-alternatives和/或在~./bashrc使用别名: alias python=/usr/local/bin/python2.7

There is a way, and it is called Conda (you can install Miniconda to start with).

It lets you create virtual environments in which you can specify the Python interpreter version you want to use. In example:

conda create -n new_environment python=3.5

Conda will download the interpreter for you, so you don't need to have it available in your system.

Appart from that, you can install packages without needing to compile them (in case they are not fully written in Python), which is something very convenient specially if you are on Windows. So, for example, conda install numpy matplotlib will not require you to compile any of those packages.

I guess you're talking about using Python under Windows because you mention the PATH variable. Recent versions of Python3 ship with the so-called Python launcher. You can run py -2 in order to start a Python2 interpreter and py -3 in order to start a Python3 interpreter. I hope this answers your question.

After some more research it looks like a possible solution could have been pyenv with the usage described in the pyenv tutorial but it only recognizes a single system-wide python runtime (whichever is the default at the moment), and doesn't provide the option to switch between the system-wide python2 and python3.

Looks like pyenv can switch only between the system python and any of the versions explicitly installed via pyenv which can all be seen via pyenv install --list and installed with eg pyenv install 3.5.2 . In other words, the python3 must be installed via pyenv in order to be able to switch between 2 and 3.

Pyenv can integrate with virtualenv which could be handy for dev testing since it includes all versions of anaconda, miniconda, pypy, jython, stackless, etc. It's probably the easiest way of installing multiple versions of python which do not come with your package manager, ie on older Linux distros that don't have a modern python in their repos.

But in the long run, all things considered, I found that the solution proposed by metatoaster is simpler and totally meets my requirements since I can use the python2 virtualenv to create both python2 and python3 environments without any overhead:

python -V
Python 2.7.12
mkdir -p ~/.virtualenvs && cd ~/.virtualenvs
virtualenv -p /usr/bin/python3 mypy3env
workon mypy3env
python -V
>>> Python 3.5.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM