Ubuntu Hacks — Working with multiple python versions on same system

1. Creating python virtual environments

Virtual environments are very prominent:

  • if you are working on some libraries which clashes with each others versions.
  • if you want to work on different versions of same library for different projects.

Creating virtual environments on Ubuntu:

$ sudo apt update
$ sudo apt install python-pip  
$ pip install virtualenv
$ virtualenv --version
$ virtualenv my_name
$ virtualenv -p /usr/bin/python3 virtualenv_name
$ source virtualenv_name/bin/activate
(virtualenv_name)$ deactivate

Limitations of using virtual environment :

  • Virtual environments are not apt for building a projects which demands different versions of python within.
  • Upgrading the version of python may lead to the data loss stored inside the virtual environment.
  • The packages and libraries stored within virtualenv can’t be used outside it. So you need to repeat installation work in order to use those packages for some other projects. This is time and memory consuming.

2. Creating Pyenv

pyenv does…

  • 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.

PyEnv is install per user, where you can use these different ways:

  • global: default for when starting a shell for the current user
  • shell: updates the current session
  • local: use the local project version specified in a .python-version file. Python is set at the current directory level, useful for managing python per project.

Installation:

$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bashrc
$ exec "$SHELL"
$ 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
$ pyenv install 2.7.8

Setting up versions:

pyenv local

$ pyenv local 2.7.6
$ pyenv local --unset

pyenv local (advanced)

$ pyenv local 2.7.6 3.3.3
$ pyenv versions
system
* 2.7.6 (set by /Users/yyuu/path/to/project/.python-version)
* 3.3.3 (set by /Users/yyuu/path/to/project/.python-version)
$ python --version
Python 2.7.6
$ python2.7 --version
Python 2.7.6
$ python3.3 --version
Python 3.3.3
$ pyenv local 3.3.3 2.7.6
$ pyenv versions
system
* 2.7.6 (set by /Users/yyuu/path/to/project/.python-version)
* 3.3.3 (set by /Users/yyuu/path/to/project/.python-version)
venv27
$ python --version
Python 3.3.3
$ python2.7 --version
Python 2.7.6
$ python3.3 --version
Python 3.3.3

pyenv global

$ pyenv global 2.7.6

pyenv global (advanced)

$ pyenv global 2.7.6 3.3.3
$ pyenv versions
system
* 2.7.6 (set by /Users/yyuu/.pyenv/version)
* 3.3.3 (set by /Users/yyuu/.pyenv/version)
$ python --version
Python 2.7.6
$ python2.7 --version
Python 2.7.6
$ python3.3 --version
Python 3.3.3
$ pyenv global 3.3.3 2.7.6
$ pyenv versions
system
* 2.7.6 (set by /Users/yyuu/.pyenv/version)
* 3.3.3 (set by /Users/yyuu/.pyenv/version)
venv27
$ python --version
Python 3.3.3
$ python2.7 --version
Python 2.7.6
$ python3.3 --version
Python 3.3.3

pyenv shell

$ pyenv shell pypy-2.2.1
$ pyenv shell --unset
$ export PYENV_VERSION=pypy-2.2.1pyenv shell (advanced)
$ pyenv shell 2.7.6 3.3.3
$ pyenv versions
system
* 2.7.6 (set by PYENV_VERSION environment variable)
* 3.3.3 (set by PYENV_VERSION environment variable)
$ python --version
Python 2.7.6
$ python2.7 --version
Python 2.7.6
$ python3.3 --version
Python 3.3.3
$ pyenv shell 3.3.3 2.7.6
$ pyenv versions
system
* 2.7.6 (set by PYENV_VERSION environment variable)
* 3.3.3 (set by PYENV_VERSION environment variable)
venv27
$ python --version
Python 3.3.3
$ python2.7 --version
Python 2.7.6
$ python3.3 --version
Python 3.3.3

Limitations of pyenv :

  • Pyenv is not an apt solution for package management.
  • You can not use two versions of same package for different projects. In order to do so, you are required to build a virtualenv using the plugin of pyenv on top of it.

3. Switching versions using GNOME terminal

sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get updatesudo apt-get install python3.6
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2
sudo update-alternatives --config python3
python3 -V
sudo rm /usr/bin/python3sudo ln -s python3.5 /usr/bin/python3

Limitations of configuring through GNOME terminal:

/usr/bin/python
/usr/lib/python3.4/usr/lib/python2.7

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store