This is a quick recipe to improve dramatically the time needed to create Python virtual environments using the new wheel binary package format.
First, make sure you have a pip version that supports wheel:
pip install --user --upgrade pip
Install wheel package:
pip install --user --upgrade wheel
I recommend to install packages in the user scheme and not system wide. This way, you don't need administrative rights, the system is not polluted with unneeded packages or broken dependencies and it's easier to reinstall if something goes wrong.
So summing up:
- apt-get ... or yum ... for system wide standard packages.
- pip install --user ... for general tools like pip, wheel and virtualenv. This way you will always have the latest versions without need to wait that they will be packaged for your distribution.
- source venv/bin/activate && pip install ... to have an isolated environment for development.
This step is common for user and virtualenv installed packages. We are creating a repository of wheel packages:
pip wheel --wheel-dir=$HOME/.wheelhouse psycopg2
It also builds its dependencies if any.
It is possible to use a requirements.txt file:
pip wheel --wheel-dir=$HOME/.wheelhouse -r requirements.txt
Notice that you can specify package version so a different wheel file will be generated:
$ ls .wheelhouse/psycopg2* .wheelhouse/psycopg2-2.4.5-cp27-none-linux_x86_64.whl .wheelhouse/psycopg2-2.5.1-cp27-none-linux_x86_64.whl
Installing from the local wheel repository
To use our brand new repository of wheel packages we must indicate pip to use it:
pip install --use-wheel --no-index --find-links=$HOME/.wheelhouse psycopg2
To install all requirements of a project:
pip install -r requirements.txt --use-wheel --no-index --find-links ~/.wheelhouse
Look at this screen capture, just 0.23 seconds:
(venv) $ time pip install --use-wheel --no-index --find-links=$HOME/.wheelhouse psycopg2 Ignoring indexes: https://pypi.python.org/simple/ Downloading/unpacking psycopg2 Installing collected packages: psycopg2 Successfully installed psycopg2 Cleaning up... pip install --use-wheel --no-index --find-links=$HOME/.wheelhouse psycopg2 0.23s user 0.05s system 98% cpu 0.285 total
- Wheel format PEP http://www.python.org/dev/peps/pep-0427/
- Wheel documentation http://wheel.readthedocs.org/en/latest/
- How to use pip to install wheel packages http://www.pip-installer.org/en/latest/cookbook.html#building-and-installing-wheels