Managing multiple Python versions with pyenv
pyenv
is a tool for managing multiple Python versions. Even if you already have Python installed on your system, it is worth having pyenv
installed so that you can easily try out new language features or help contribute to a project that is on a different version of Python.
Using pyenv
to Install Python
Now that you have pyenv
installed, installing Python is the next step. You have many versions of Python to choose from. If you wanted to see all the available CPython 3.6 through 3.8, you can do this:
$ pyenv install --list | grep " 3\.[678]" 3.6.0 3.6-dev 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.7.0 3.7-dev 3.7.1 3.7.2 3.8-dev
The above shows all the Python versions that pyenv
knows about that match the regular expression. In this case, that is all available CPython versions 3.6 through 3.8. Likewise, if you wanted to see all the Jython versions, you could do this:
$ pyenv install --list | grep "jython" jython-dev jython-2.5.0 jython-2.5-dev jython-2.5.1 jython-2.5.2 jython-2.5.3 jython-2.5.4-rc1 jython-2.7.0 jython-2.7.1
Again, you can see all the Jython versions that pyenv
has to offer. If you want to see all the versions, you can do the following:
$ pyenv install --list ... # There are a lot
Once you find the version you want, you can install it with a single command:
$ pyenv install -v 3.7.2 /tmp/python-build.20190208022403.30568 ~ Downloading Python-3.7.2.tar.xz... -> https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz Installing Python-3.7.2... /tmp/python-build.20190208022403.30568/Python-3.7.2 /tmp/python-build.20190208022403.30568 ~ [...] Installing collected packages: setuptools, pip Successfully installed pip-18.1 setuptools-40.6.2 Installed Python-3.7.2 to /home/evren/.pyenv/versions/3.7.2
This will take a while because pyenv
is building Python from the source, but once it’s done, you’ll have Python 3.7.2 available on your local machine. If you don’t want to see all the output, remove the -v
flag. Even development versions of CPython can be installed:
$ pyenv install 3.8.0
For the rest of the tutorial, the examples assume you’ve installed 3.6.8
and 2.7.15
, but substitute these values for the Python versions you installed. Also, note that the system Python version in the examples is 2.7.12
.
Using Your New Python
Now that you’ve installed a couple of different Python versions, let’s see some basics on how to use them. First, check what versions of Python you have available:
$ pyenv versions * system (set by /home/evren/.pyenv/version) 3.8.0 3.12.0
The *
indicates that the system
Python version is active currently. You’ll also notice that this is set by a file in your root pyenv
directory. This means that, by default, you are still using your system Python:
$ python -V Python 3.12.3
If you try to confirm this using which
, you’ll see this:
$ which python ~/.pyenv/shims/python
This might be surprising, but this is how pyenv
works. pyenv
inserts itself into your PATH
and from your OS’s perspective is the executable that is getting called. If you want to see the actual path, you can run the following:
$ pyenv which python /usr/bin/python
If, for example, you want to use version 3.8.0, then you can use the global
command:
$ pyenv global 3.8.0 $ python -V Python 3.8.0 $ pyenv versions system * 3.8.0 (set by /home/evren/.pyenv/version) 3.12.0
If you ever want to go back to the system version of Python as the default, you can run this:
$ pyenv global system $ python -V Python 3.12.3
You can now switch between different versions of Python with ease.