Multiple versions of Python

Do you have multiple versions of Python to be installed? Do you have additional python packages that must be used in separate versions of Python installed on the system?

Use Virtual Environments!

Today we will see how to setup 2 different versions of Python environment.

  • Install python

    sudo apt install python

  • Install pip (python’s package management system):

    sudo apt install python-pip

  • Upgrade pip:

    pip install –upgrade pip

  • Install virtualenvwrapper by running this command:

    pip install virtualenvwrapper

  • Before we can create a virtualenv, we need to run the virtualenvwrapper script. We can find the location of virtualenvwrapper script by running the following command:

    which virtualenvwrapper.sh

  • Execute the script as:

    source ~/.local/bin/virtualenvwrapper.sh

  • Make a virtual environment with following command

    mkvirtualenv -p <location of python3.5> <name_of_env>

    For example for creating virtual env for python 3.5 named ‘venv35’, run:

    mkvirtualenv -p python3.5 venv35

    It will take activate the virtualenv for us directly. To deactivate it, enter:

    deactivate

  • Similarly, we can create another environment for python 2.7 named ‘venv27’, run:

    mkvirtualenv -p python2.7 venv27

  • To run, any environment, run the command:

    workon <env name>

    For exaemple, to run venv27, use:

    workon venv27

Enjoy!

Advertisements