Python

How to Switch Python Version on Linux Using Pyevn

Do you need to run a piece of software that requires a different version of Python than the one you have installed on your system? There are several ways you can solve this problem, but the easiest one relies on a tool called pyenv. Let’s briefly discuss what pyenv does and why you should use it before explaining exactly what you need to do to switch the default Python version with it.

What Is pyenv and How Does It Work?

pyenv is a Python version management tool made from pure shell scripts, which means it doesn’t depend on Python. That’s nice because the last thing you want is to pollute your environment by installing a tool that’s supposed to help you manage it better.

The beauty of pyenv comes from the fact that it simply adds a directory at the top of your $PATH, a variable that tells the Linux operating system where to look for executables, such as Python. This new directory contains v a lightweight executable that passes your command along to pyenv.

You can use the following command to display your current $PATH:

$ echo $PATH

You should see multiple directories separated by colons:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

To make the output nicer, you can use a more sophisticated version of the command above:

$ echo -e ${PATH//:/\\n}
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
/usr/games
/usr/local/games

When you use the python command to launch a piece of software, the operating system goes through the directories in the $PATH variable from top to bottom until it finds one containing the relevant executable.

To quickly see which directory that is, use the whereis command:

$ whereis python
/usr/bin/python

The python -V command can tell you which version of Python is installed:

$ python -V
Python 2.7.18

To let you use any version of Python you want without messing with the one that came installed with your operating system, pyenv manipulates the $PATH variable, allowing you to change the global Python version on a per-user basis easily. What’s more, you can also use specific versions of Python for individual projects or set a particular version of Python only for your current shell session.

How to Install pyenv?

Before you install pyenv itself, you should make sure that there are no unmet system dependencies:

$ sudo apt-get update; sudo apt-get install make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev git wget curl llvm libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

Once that’s done, you can use the automatic installer to install pyenv on your Linux system:

$ curl https://pyenv.run | bash

Then you need to add the following lines to your ~/.bashrc or equivalent configuration file if you’re using a different shell than Bash:

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Don’t worry; the automatic installer will tell you exactly what you need to add and where. Finally, you can restart your shell (close your terminal) and verify that pyenv has been successfully installed by displaying its current version:

$ pyenv --version
pyenv 2.2.4

How to Switch Python Version Using pyenv?

To switch the Python version using pyenv, you need first to install at least one version of Python using the pyenv install command. Simply type the desired version after the command:

$ pyenv install 3.10.2

To display a list of all available versions, do the following:

$ pyenv install --list

There are a lot of different versions to choose from, so we won’t copy & paste them here to spare your scroll wheel the effort it would take to go through them all.

Since the Python print version command (python -V) we used earlier in this article would only tell you what the system Python version is, you need to use the pyenv versions command to see which versions are now available to pyenv:

$ pyenv versions
* system (set by /home/david/.pyenv/version)
  2.7.17
  3.7.10
  3.10.2

As you can see, pyenv is now aware of three versions of Python: the system version (2.7.18 in our case), 2.7.17, 3.7.10, and 3.10.2.

Now, switching Python version on the global, local, or shell basis is a matter of single command:

$ pyenv global 2.7.17
$ pyenv local 3.7.10
$ pyenv shell 3.10.2

In our case, we used the global command to downgrade Python version 2.7.18 to version 2.7.17. To verify that we’ve accomplished the desired result, we can use two familiar commands:

$ pyenv versions
system
* 2.7.17 (set by /home/david/.pyenv/version)
  3.10.2
  3.7.10

and

$ python -V
Python 2.7.17

That’s how easy it is to downgrade the Python version using pyenv! After a while, you might want to delete a few versions of Python that you no longer need. The following command lets you do just that:

$ pyenv uninstall <version>

You can see more useful pyenv commands by typing pyenv help in the terminal. We also recommend you read the official documentation on GitHub to understand better how pyenv works under the hood.

Conclusion

Switching or downgrading the Python version is something virtually all developers and even some advanced Linux users have to do at some point. Fortunately, tools like pyenv make the task effortless, allowing you to accomplish the desired goal with a few simple commands.

About the author

David Morelo

David Morelo is a professional content writer in the technology niche, covering everything from consumer products to emerging technologies and their cross-industry application