Python on Debian Wheezy
This article describes the installation and usage of professionalPython development environments inDebianWheezy. Thefollowing principles are followed:
- Python installations for development are installed locally. Pythonpackages installed by the package manager arenot usedfor development.
- Projects can use specific versions of Python, not just the versioninstalled 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
tokeep 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 improvementshave 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 incurrent use. Notably, many people continue to use Python 2, as Python 3is 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 virtualenvironment setup, but the resulting virtual environments can be used inthe 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 versionsof Python that this article covers.
Pip is used for Pythonpackage management. A download cache location can be specified using aconfiguration 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 toinstall from thepython.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, seeVirtual Environment Setup: pyvenv below.
Installation: Python 3.3.x
Download the gzipped source archive for the version(s) that you want toinstall from thepython.org website.If you have not already done so, also download the gzipped archive for thelatest release of setuptools from thesetuptools 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, seeVirtual 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 toinstall from thepython.org website.If you have not already done so, also download the gzipped archive for thelatest release of setuptools from thesetuptools 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, andvirtualenv asfollows:
$ 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, seeVirtual Environment Setup: virtualenv below.
Installation: Python 2.x.x
Download the gzipped source archive for the version(s) that you want toinstall from thepython.org website.If you have not already done so, also download the gzipped archive for thelatest release of setuptools from thesetuptools 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, andvirtualenv asfollows:
$ 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 thevirtual environment is ready forusage.
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 thisversion, so install it viasetuptools asfollows:
$ 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 forusage.
Virtual Environment Setup: virtualenv
We have to usevirtualenv in olderversions of Python, which do not includepyvenv
. Note thatvirtualenv
is used in the same way; it is just not aslight-weight. Setup avirtualenv
virtual environment asfollows:
$ cd ~/projects/example $ /usr/local/opt/python-2.7.8/bin/virtualenv virtualenv-2.7.8
Pip is included, so thevirtual environment is ready forusage.
Usage
Each project can contain any number of virtual environments, each of whichcontains a different version of Python. To activate an environment,source theactivate
script as follows:
$ cd ~/projects/example $ source virtualenv-3.3.5/bin/activate
Note that you must install any dependencies in each virtual environmentseparately. For example, if your project has asetup.py
script, runningpython setup.py develop
will prepare theenvironment for development as well as install the specified dependencies.If you have arequirements.txt
file, runningpip install -r requirements.txt
will install the specifieddependencies.
To deactivate an environment, simply run the deactivate
function as usual.
The contents of the virtual environment directories should not be includedin revision control, so be sure to ignore them(/virtualenv*
). If virtual environments are a standard partof 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 installationdirectory 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 nolonger work. Before removing an installation, you may therefore want tosearch for any projects that have such a virtual environment and notifythe users of your intention to remove the installation. For example, youcan 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
).
ref:http://www.extellisys.com/articles/python-on-debian-wheezy