Ansible Automation

RedHat

Manage Multiple Python versions: The easy way!

In today's fast pacing development environment, where Python has turned out to be the most loved and supported language for any kind of development scenarios. One of the major trouble faced by Python developers like me is to manage multiple versions of Python, because currently even if the Python latest versions be it 2.7.16 or 3.7.4 (latest Python versions available at the time of writing the blog). User's all across the globe still manage applications and code written in as old as Python 2.0-2.5.

Before we dive into actual configuration tasks, I want to briefly discuss about following two terms:

1. Multiple Python versions: Maintaining multiple versions of Python on the same development/local machine.

2. Virtual Environment (venv): A virtual environment is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python, i.e., one which is installed as part of your operating system.

Without further ado, we'll now dive and work through the steps and procedures to configure the user's development environment to use multiple Python versions. Also, as mentioned in the blog title configuring the easy way is because when I had to configure my system I had to browse several blogs and results to get my setup up and running, but going through this single blog, user can make their system up and running with multiple Python versions.

Pre-requisites

  • Linux/Mac OS system

How to implement?

These two tools are required to make your system up and running with multiple Python Virtual environment:

  • venv
  • pyenv
  • pyenv-virtualenv

1. venv

The venv module provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (which matches the version of the binary that was used to create this environment) and can have its own independent set of installed Python packages in its site directories.

NOTE: The pyvenv script has been deprecated as of Python 3.6 in favor of using python3 -m venv to help prevent any potential confusion as to which Python interpreter a virtual environment will be based on.

But, for setting multiple Python virtual environment we'll not be going through venv road. So, if you want to get more information with respect to venv you can visit following link to get more information venv.

2. pyenv

Also, known as Simple Python Version Management pyenv lets you easily switch between multiple versions of Python. It's simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well.

pyenv can do following things:

  • Let you change the global Python version on a per-user basis.
  • Provide support for per-project Python versions.
  • Allow you to override the Python version with an environment variable.
  • Search commands from multiple versions of Python at a time. This may be helpful to test across Python versions with tox.

Above article sourced from pyenv github page. Pyenv documentation includes great description and details of how pyenv works but in this article we'll go through how to use pyenv for making your environment up and running in no time.

First thing first, you'll need to install pyenv:

  • Mac OS
  • $ brew update
    $ brew install pyenv
  • Linux
  • $ curl https://pyenv.run | bash

    and this is equivalent to:

    $ curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash

Next, add the following towards the bottom of your shell scripts to allow pyenv to automatically change versions for you. Please make sure eval is placed toward the end of the shell configuration file since it manipulates PATH during the initialization (fyi: I have Mac environment).

$ export PYENV_ROOT="$HOME/.pyenv" >> ~/.bash_profile
$ export PATH="$PYENV_ROOT/bin:$PATH" >> ~/.bash_profile
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
  • Zsh note: Modify your ~/.zshenv file instead of ~/.bash_profile.
  • fish note: Use pyenv init - | source instead of eval (pyenv init -).
  • Ubuntu and Fedora note: Modify your ~/.bashrc file instead of ~/.bash_profile.

Running pyenv versions will show which Python pyenv versions are currently installed, with a * next to the one currently in use. pyenv version shows this directly, and python --version can be used to verify this.

Sample Out:

$ pyenv versions
* system (set by /Users/Sumit/.pyenv/version)

Now, to install an additional pyenv version, for e.g. say Python version 2.6.9, simply use pyenv install 2.6.9.

Once installed if you fire pyenv versions again, now it should show latest 2.6.9 version listed as well, like:

$ pyenv versions
* system (set by /Users/Sumit/.pyenv/version)
  2.6.9

3. pyenv-virtualenv

pyenv-virtualenv is a tool to create virtual environments integrated with pyenv, and works for all versions of Python. It is still recommended to use the official Python venv where possible. But if, for example, you’re creating a virtual environment based on 2.7.13, then this compliments pyenv.

Installation is same as pyenv.

$ brew install pyenv-virtualenv

Add following line towards the bottom of respective files based on terminal:

$ eval "$(pyenv init -)"
$ eval "$(pyenv virtualenv-init -)"
  • Zsh note: Modify your ~/.zshrc file instead of ~/.bash_profile.
  • Ubuntu and Fedora note: ~/.bashrc/ ~/.bash_profile.

These lines help pyenv to activate and deactivate environments directly when traversing through directories.

Now, to create a new virtual python environment:

$ pyenv virtualenv [version] [name-to-give-it]

for example:

$ pyenv virtualenv 2.6.9 test_virtual_env_2.6.9

Now, to check what all environments are present you can check wuth the command:

$ pyenv virtualenvs

To activate the installed python virtualenv:

$ pyenv activate [name]

for example:

$ pyenv activate test_virtual_env_2.6.9

To de-activate the installed python virtualenv:

$ pyenv deactivate

Un-installation

To remove the installed Python virtual environment, you'll need to run following command:

$ pyenv uninstall [installed_version_to_be_removed]

for example:

$ pyenv uninstall 2.6.9

And, this should clear and uninstall the Python pyenv version installed on your development machine.

Final Thoughts:

Above configuration should be enough to make your environment up and running and ready with multiple Python versions and you can switch between different availble Python versions based on your development need and requirements.

What a computer is to me is it's the most remarkable tool that we have ever came up with. It's equivalent of a bicycle for our minds. - Steve Jobs

Make it more awesome

Now, as you have got the basic understanding of how to setup your system and run multiple Python virtual environment, build and test your Python based applications via supported Python versions.

Article By Sumit Jaiswal

Currently works @ Ansible by Redhat as SSE.

Discuss about post

Subscribe to my weekly newsletter