Manage multiple Python Versions with Pyenv

How to Manage multiple Python Versions with pyenv

As we know python is an unavoidable programming language when it comes to Infrastructure automation or IT Operations. Directly or indirectly Python plays an important role in DevOps as it is the popular backend programming language say, Docker, Puppet, Ansible, and more. But the painful part is the different versions of python in the operating system. Many Operating System comes with default python 2.x and some automation requires python 3.x. To mitigate these issues, we have a Simple Python Version management Tool called the pyenv. In this article, we will discuss How to Manage multiple Python Versions with the pyenv.

How Pyenv works?

In the background, Pyenv injects Shim executables into the PATH to define which version of python needs to be used while you run the python commands. So how this PATH thing works? So, whenever you run python or pip command, your OS platform will search the executable in the list of executable directories called PATH. This PATH is the list of directories that is separated with a colon like /usr/local/bin:/usr/bin:/bin. This will help us to Manage multiple Python Versions with the pyenv.

So, as we discussed, the pyenv uses Shims which are lightweight executables that will pass the commands with the pyenv executable ($(pyenv root)/shims:/usr/local/bin:/usr/bin:/bin). This will work in the following order.

How pyenv works with Shims
How pyenv works with Shims

So, whenever you execute the shim, the pyenv will determine the python version you should be using by following order.

  1. Environment Variable: PYENV_VERSION is the environment variable that needs to be set with the needed python version.
  2. Locally, Current Directory file: keep the file called .python-version in the current working directory. This can be modified by passing the command called pyenv local.
  3. Globally: the python version can be modified globally by using the command call pyenv global.

If none of the above options are not available, the python will use the System default python version.

How to Install Pyenv on various Platforms?

Now lets discuss the Installation if pyenv on various platforms. Before that, we will discuss the Requirements of the pyenv.

Prerequisites

Lets quickly discuss the Dependencies and Prerequisites in the below list

  • MacOS : use brew – brew install openssl readline sqlite3 xz zlib
  • Debian: use Apt-get – sudo apt-get update; sudo apt-get install --no-install-recommends make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
  • CentOS: use yum – yum install gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel tk-devel libffi-devel
  • openSUSE: use zipper: zypper install gcc automake bzip2 libbz2-devel xz xz-devel openssl-devel ncurses-devel readline-devel zlib-devel tk-devel libffi-devel sqlite3-devel

Using pyenv-installer

Once you installed all the dependencies as mentioned in the above section, you can go and straightaway runs the following command to install the pyenv.

$ curl https://pyenv.run | bash

Then, don’t forget to add the following lines in to the .bashrc file.

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

Then, restart the terminal session to make it effective.

Manage Multiple Python versions.

Just like any other package manager, using pyenv, you can install a specific version of python and manage the versions in your operation system. Let’s see them one by one.

Install Python using pyenv

So, you can use the install command and following that the version number to install the python. Before that, you can see all the available versions of python by running the following command

$ pyenv install --list
Available versions:
  2.1.3
  2.2.3
  2.3.7
  2.4.0
  2.4.1
  2.4.2
  2.4.3
  2.4.4
  2.4.5
  2.4.6
  2.5.0
  2.5.1
  2.5.2
  2.5.3
  2.5.4
  2.5.5
  2.5.6
  2.6.6
  2.6.7
  2.6.8
  2.6.9
  2.7.0
  2.7-dev
  2.7.1
  2.7.2
  2.7.3
  2.7.4
  2.7.5
  2.7.6
  2.7.7
  2.7.8
  2.7.9
  2.7.10
  2.7.11
  2.7.12
  2.7.13
  2.7.14
  2.7.15
  2.7.16
  2.7.17
  2.7.18
  3.0.1
  3.1.0
  3.1.1
  3.1.2
  3.1.3
  3.1.4
  3.1.5
...
...
... # more....

Then If you want to install the specific version from the list, use the following command.

$ pyenv install -v 3.7.2
/tmp/python-build.20210408021412.23512 ~
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.20210408021412.23512/Python-3.7.2 /tmp/python-build.20210408021412.23512 ~
[...]
Installing collected packages: setuptools, pip
Successfully installed pip-18.1 setuptools-40.6.2
Installed Python-3.7.2 to /home/vagrant/.pyenv/versions/3.7.2

You can see all your installed version of python by listing the following directory location

$ ls ~/.pyenv/versions/
2.7.15  3.7.2  3.10-dev

If you want to uninstall the specific version of python, you can simply remove the directory of the specific version or you can use the following command.

$ rm -rf ~/.pyenv/versions/3.10-dev

OR

$ pyenv uninstall 3.10-dev

Managing default python using pyenv

Once you install and keep the needed list of python on your pyenv list, you can chose and set the default version of the python using pyenv commands. To see the list of installed python versions, use the following command

$ pyenv versions
* system (set by /home/vagrant/.pyenv/version)
  2.7.15
  3.7.2

Now if you want to keep the python version 3.7.2 as the system default, you can simply use the following commands.

$ pyenv global 3.7.2

Now go ahead and check the python version like we normally do with python command and you will see the default version changed.

$ python -V
Python 3.7.2

And the same can be seen on the pyenv listing.

$ pyenv versions
  system (set by /home/vagrant/.pyenv/version)
  2.7.15
* 3.7.2

So the asterisk pointed is the default version of the python which is install and set default by pyenv globally.

Some more Important commands are:

  • Set python version locally: $ pyenv local 3.7.2
  • Set back System Python version: $ pyenv global system
  • Set Python shell specific: $ pyenv shell 3.7.2
  • Create Virtualenv out of a specific version of python: $ pyenv virtualenv 3.7.2 env_name
  • Activate your Virtual environment: $ pyenv activate env_name
  • Deactivate the virtual environment: $ pyenv deactivate
  • See the installed location of the python: $ pyenv which python

Similarly, pyenv can do much more things apart from basic version management. Like, managing multiple versions of python in a different virtual environment and Managing multiple versions of python Simultaneously. We will discuss more features of pyenv in our upcoming article.

Conclusion

In this article, we have discussed How to Manage multiple Python Versions with Pyenv. In the upcoming article, we will discuss How to manage multiple versions of python in different virtual environments and How to manage multiple versions of python Simultaneously. If you have questions and doubts about the Installation or Concepts or in the procedure, feel free to write a comment on this article. Stay tuned and subscribe DigitalVarys for more articles and study materials on DevOpsAgileDevSecOps, and App Development.

1 thought on “How to Manage multiple Python Versions with pyenv”

  1. It is now the need of market. Smartphone App Development for healthcare, travel, manufacturing or any other industry is most important thing, this unique and very accurate information about the web development and coding help so much. Subscribed your blog for more blog updates.

Leave a Reply