Setting up virtual environments for Python is always a great way to keep your projects and their related Python packages independent.
Virtualenv is the de-facto standard choice for Python environments. It’s capable of creating independent Python module groupings which you can switch to and from using commands. Unfortunately, virtualenv does not install and manage multiple versions of Python, but we’ll get to a solution for that with pythonbrew later.
To install virtualenv, simply use pip:
If you would like bash completion, you can use the script created by Eugene Kalinin:
You may now use virtualenv and install packages within environments as either root or an ordinary user on the system.
Create an environment as follows:
You now have a local instance of Python which can be found at ./python-env/bin/python
You may then go ahead and install packages in the current directory by using the virtualenv’s pip executable.
All packages will placed in the local virtualenv under ./python-env/lib/python2.7/site-packages/
Rather than having to type such nasty pathnames to get to pip and python, virtualenv has a cool little activate script which will alter your entire working environment so that you can use all tools as you would if they were installed on the system.
You may now use the tools as per usual:
To return to your normal environment, simply type deactivate:
I suggest adding the following to the end of your .bashrc to help simplify activation of virtualenvs:
You may now type the following to activate a virtual environment for Python:
Pythonbrew Installation & Usage
Pythonbrew takes virtualenv a lot further, allowing for installation of various Python versions and management more akin to the amazing RVM for Ruby.
Install pythonbrew as follows:
Since pythonbrew will build Python from source, we need to be prepared:
Installing Python versions may be done as follows:
It’s a good idea to switch on verbose mode if you want to see what’s going on:
To view the newest Python version for each sub-version and get an idea what you can install:
Permanently switch to a Python version as follows:
Or temporarily switch to a Python version in the current session:
Jump back to the system’s Python version by using:
You may also view all installed Python versions:
Uninstall a version of Python as follows:
To upgrade pythonbrew to the newest version:
You may run your Python script against all installed versions of Python using:
Pythonbrew Environments Using Virtualenv
Pythonbrew can now use virtualenv to create environments. Install virtualenv for pythonbrew as follows:
Creating a project:
Much like RVM, projects are related to each version of Python that’s installed. Thus, you may have a project called “hello” twice, one against version 2.7.3 and one against 3.2.3 if you like.
List your projects
To switch to a project, type:
To de-activate the environment:
To delete an environment:
You may install packages using pip which is available independently for each environment automatically.
Pythonbrew has so much potential and could easily be as good as RVM, but there are a few problems:
- Lack of bash completion
- No auto-switching of profiles
- Lack of support for PyPy and Jython: Although there’s a fork of the project (pythonz) which adds this support, the fork is based on a very old version of pythonbrew. PyPy is the future of Python and Jython is really handy when you need Java integration with Python, so this functionality would be great.
- Project abandoned?: Check out the number of pull requests pending at Github (many of which are really useful).
If I have some spare time one day, I’ll attempt to tackle these problems so that us Python-heads can have all bells and whistles in this otherwise excellent application.