Python is a great language for general programing, but when combined with the Django framework, Web development in Python becomes a breeze.

But managing different python versions and packages can start become a real pain. If you are following a tutorial online for instance or you want to run a particular python app that works with particular version of python, then things can get hairy. You have to synchronize your python environment together with the developed app.

Luckily, there are tools to manage various Python versions. We shall use Pyenv and Virtualenv to manage various python versions and packages giving us an isolated workspace from the global system python library.

Pyenv

Pyenv enables you run multiples versions of python on the same machine. It accomplishes this by intercepting Python commands using shim executables injected into your PATH.

Installation

Clone pyenv from github into wherever you like. For this case, we close it to ~/.pyenv.

$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv

Now we make sure that our shell understands pyenv binaries at boot by adding it ~/.bash_profile.

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

Now pyenv is installed. Now we can install a new version of python by running
pyenv install 3.6.0.

We can then use virtualenv to create a virtual python environment where we determine the python environment we want and packages.

Virtualenv

virtualenv is a tool to create isolated Python environments. We can install virtualenv using python pip, the language's package manager like so.

pip install virtualenv.

If you wish to install virtualenv globally, then you can run the command as root like so sudo pip install virtualenv or sudo apt-get install python-virtualenv.

To create a new virtual environment, create your project/app folder and then run the virtualenv command over it. Then activate your new virtual environment;

mkdir ~/workspace/new_app
virtualenv ~/workspace/new_app
source ~/workspace/new_app/bin/activate

Now you can install python packages specific to your project that are isolated from the global python library.

Now I know you are dying for an easy way out. Instead of doing all this steps manually, you could just run the pyenv-installer. It will install pyenv as well as virtualenv or actually the pyenv-virtualenv pyenv plugin. It combines pyenv and virtualenv so that you can install and manager different python versions as well as isolate python packages on a project basis.

Here's an example of a bash script that installs pyenv, then downloads and installs python 3.6.0, creates a virtual environment called django1.8, activates it and then installs django 1.8 to that environment.

#!/bin/bash
curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
cat <<'EOF' >  /home/vagrant/.bash_profile
export PATH="/home/vagrant/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
EOF
source /home/vagrant/.bash_profile
pyenv install 3.6.0
pyenv virtualenv 3.6.0 django1.8
pyenv activate django1.8
pip install django==1.8

Or you can run it interactively on the terminal.

#list available virtual enviroments
[email protected] % pyenv virtualenvs 
  3.6.2/envs/lordi (created from /home/oquidave/.pyenv/versions/3.6.2)
  venv27 (created from /home/oquidave/.pyenv/versions/2.7)
* venv3.6 (created from /home/oquidave/.pyenv/versions/3.6.2)

(venv3.6) [email protected] (master) [1] % pyenv virtualenv 3.6.2 django2.0
Using base prefix '/home/oquidave/.pyenv/versions/3.6.2'
New python executable in /home/oquidave/.pyenv/versions/3.6.2/envs/django2.0/bin/python3.6
Also creating executable in /home/oquidave/.pyenv/versions/3.6.2/envs/django2.0/bin/python
Installing setuptools, pip, wheel...done.
Requirement already satisfied: setuptools in /home/oquidave/.pyenv/versions/3.6.2/envs/django2.0/lib/python3.6/site-packages
Requirement already satisfied: pip in /home/oquidave/.pyenv/versions/3.6.2/envs/django2.0/lib/python3.6/site-packages
pyenv: cannot rehash: /home/oquidave/.pyenv/shims/.pyenv-shim exists

[email protected] (master) % pyenv activate django2.0

(django2.0) [email protected] (master) % pip install django==2.0

Collecting django==2.0
  Using cached https://files.pythonhosted.org/packages/44/98/35b935a98a17e9a188efc2d53fc51ae0c8bf498a77bc224f9321ae5d111c/Django-2.0-py3-none-any.whl
Collecting pytz (from django==2.0)
  Downloading https://files.pythonhosted.org/packages/dc/83/15f7833b70d3e067ca91467ca245bae0f6fe56ddc7451aa0dc5606b120f2/pytz-2018.4-py2.py3-none-any.whl (510kB)
    100% |████████████████████████████████| 512kB 420kB/s 
Installing collected packages: pytz, django
Successfully installed django-2.0 pytz-2018.4
pyenv: cannot rehash: /home/oquidave/.pyenv/shims/.pyenv-shim exists

(django2.0) [email protected] (master) [1] % python --version                                  
Python 3.6.2

(django2.0) [email protected] (master) % django-admin version
2.0

Image: Pixabay

Further reading: