Python on Debian Wheezy

This article describes the installation and usage of professional Python development environments in Debian Wheezy. The following principles are followed:

  • Python installations for development are installed locally. Python packages installed by the package manager are not used for development.
    • Projects can use specific versions of Python, not just the version installed by the package manager.
    • System updates do not affect development.
    • Development updates do not affect the system.
  • Multiple versions of Python can be installed and used simultaneously.
    • Different projects can use different, specific versions of Python.
    • Projects can be tested for compatibility with multiple versions.
  • Installations are put into /usr/local/opt to keep the filesystem well-organized.
  • All development is done within project-specific virtual environments.
    • Updates of a project's dependencies do not affect other projects.

Overview

Python has gone through a number of changes, as significant improvements have been made to the language as well as its ecosystem. Unfortunately, a result of such changes is that a very wide range of versions are in current use. Notably, many people continue to use Python 2, as Python 3 is not fully backwards-compatible.

This article describes how to install Python versions 2.6 through 3.4. Different versions require different methods of installation and virtual environment setup, but the resulting virtual environments can be used in the same way regardless of the version.

Prerequisites

In order to compile Python, you must first install the prerequisites. This can be done using the package manager as follows:

$ sudo apt-get install build-essential
$ sudo apt-get install libncurses5-dev libncursesw5-dev libreadline6-dev
$ sudo apt-get install libdb5.1-dev libgdbm-dev libsqlite3-dev libssl-dev
$ sudo apt-get install libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev

These prerequisites should be sufficient for building any of the versions of Python that this article covers.

Pip is used for Python package management. A download cache location can be specified using a configuration file, an environment variable, or command-line option. Specify a download cache location using a configuration file as follows:

$ mkdir -p ~/.pip/cache
$ echo '[global]' > ~/.pip/pip.conf
$ echo 'download_cache = ~/.pip/cache' >> ~/.pip/pip.conf

Installation: Python 3.4.x

Download the gzipped source archive for the version(s) that you want to install from the python.org website.

Compile and install Python into /usr/local/opt as follows, using the version number for the version that you would like to install:

$ cd /tmp
$ tar -zxf /path/to/your/Python-3.4.1.tgz
$ cd Python-3.4.1
$ ./configure --prefix=/usr/local/opt/python-3.4.1
$ make
$ sudo make install
$ cd /tmp
$ sudo rm -rf Python-3.4.1

To setup a virtual environment, see Virtual Environment Setup: pyvenv below.

Installation: Python 3.3.x

Download the gzipped source archive for the version(s) that you want to install from the python.org website. If you have not already done so, also download the gzipped archive for the latest release of setuptools from the setuptools PyPI page.

Compile and install Python into /usr/local/opt as follows, using the version number for the version that you would like to install:

$ cd /tmp
$ tar -zxf /path/to/your/Python-3.3.5.tgz
$ cd Python-3.3.5
$ ./configure --prefix=/usr/local/opt/python-3.3.5
$ make
$ sudo make install
$ cd /tmp
$ sudo rm -rf Python-3.3.5

To setup a virtual environment, see Virtual Environment Setup: pyvenv + pip below.

Installation: Python 3.0.x, 3.1.x, 3.2.x

Download the gzipped source archive for the version(s) that you want to install from the python.org website. If you have not already done so, also download the gzipped archive for the latest release of setuptools from the setuptools PyPI page.

Compile and install Python into /usr/local/opt as follows, using the version number for the version that you would like to install:

$ cd /tmp
$ tar -zxf /path/to/your/Python-3.2.5.tgz
$ cd Python-3.2.5
$ ./configure --prefix=/usr/local/opt/python-3.2.5
$ make
$ sudo make install
$ cd /tmp
$ sudo rm -rf Python-3.2.5

Install setuptools, Pip, and virtualenv as follows:

$ sudo su
# cd /tmp
# tar -zxf /path/to/your/setuptools-5.4.2.tar.gz
# cd setuptools-5.4.2
# export PATH=/usr/local/opt/python-3.2.5/bin:$PATH
# python3 setup.py install
# easy_install pip
# pip install virtualenv
# cd /tmp
# rm -rf setuptools-5.4.2
# exit

To setup a virtual environment, see Virtual Environment Setup: virtualenv below.

Installation: Python 2.x.x

Download the gzipped source archive for the version(s) that you want to install from the python.org website. If you have not already done so, also download the gzipped archive for the latest release of setuptools from the setuptools PyPI page.

Compile and install Python into /usr/local/opt as follows, using the version number for the version that you would like to install:

$ cd /tmp
$ tar -zxf /path/to/your/Python-2.7.8.tgz
$ cd Python-2.7.8
$ ./configure --prefix=/usr/local/opt/python-2.7.8
$ make
$ sudo make install
$ cd /tmp
$ sudo rm -rf Python-2.7.8

Install setuptools, Pip, and virtualenv as follows:

$ sudo su
# cd /tmp
# tar -zxf /path/to/your/setuptools-5.4.2.tar.gz
# cd setuptools-5.4.2
# export PATH=/usr/local/opt/python-2.7.8/bin:$PATH
# python setup.py install
# easy_install pip
# pip install virtualenv
# cd /tmp
# rm -rf setuptools-5.4.2
# exit

Virtual Environment Setup: pyvenv

Setup a Python 3.4.x virtual environment as follows:

$ cd ~/projects/example
$ /usr/local/opt/python-3.4.1/bin/pyvenv virtualenv-3.4.1

Pip is included, so the virtual environment is ready for usage.

Virtual Environment Setup: pyvenv + pip

Setup a Python 3.3.x virtual environment as follows:

$ cd ~/projects/example
$ /usr/local/opt/python-3.3.5/bin/pyvenv virtualenv-3.3.5

Pip is not included in this version, so install it via setuptools as follows:

$ source virtualenv-3.3.5/bin/activate
$ tar -zxvf /path/to/your/setuptools-5.4.2.tar.gz
$ cd setuptools-5.4.2
$ python setup.py install
$ cd ..
$ rm -rf setuptools-5.4.2
$ easy_install pip
$ deactivate

With that done, the virtual environment is ready for usage.

Virtual Environment Setup: virtualenv

We have to use virtualenv in older versions of Python, which do not include pyvenv. Note that virtualenv is used in the same way; it is just not as light-weight. Setup a virtualenv virtual environment as follows:

$ cd ~/projects/example
$ /usr/local/opt/python-2.7.8/bin/virtualenv virtualenv-2.7.8

Pip is included, so the virtual environment is ready for usage.

Usage

Each project can contain any number of virtual environments, each of which contains a different version of Python. To activate an environment, source the activate script as follows:

$ cd ~/projects/example
$ source virtualenv-3.3.5/bin/activate

Note that you must install any dependencies in each virtual environment separately. For example, if your project has a setup.py script, running python setup.py develop will prepare the environment for development as well as install the specified dependencies. If you have a requirements.txt file, running pip install -r requirements.txt will install the specified dependencies.

To deactivate an environment, simply run the deactivate function as usual.

The contents of the virtual environment directories should not be included in revision control, so be sure to ignore them (/virtualenv*). If virtual environments are a standard part of the project, the ignore rule can be added to the project configuration. Otherwise, add the ignore rule to your user configuration.

Removal

To remove an installation, all you have to do is remove the installation directory from /usr/local/opt as follows:

$ sudo rm -rf /usr/local/opt/python-3.4.1

Note that any virtual environments that use a removed installation will no longer work. Before removing an installation, you may therefore want to search for any projects that have such a virtual environment and notify the users of your intention to remove the installation. For example, you can search with a command like the following:

$ find /home -type d -name virtualenv-3.4.1 2>/dev/null

Users can remove a virtual environment by deleting the directory (rm -rf virtualenv-3.4.1).