In this post I am going to introduce Python’s virtual environments, through the virtualenv tool. With this tool, you can keep separate environments for your projects, with their own dependencies and executables. By isolating the project environments, you can keep them neat and organized, without messing up your global installation. Depending on the requirements of what you’re working on, you can use different versions of Python or keep older libraries on a per-project basis. Nothing outside the virtual environment will be modified.
To begin with, install virtualenv. You can do it in several ways. Via pip: pip install virtualenv, or with easy_install: easy_install virtualenv. Now let’s look at the help menu:
root@kali:~# virtualenv -h
Usage: virtualenv [OPTIONS] DEST_DIR
--version show program's version number and exit
-h, --help show this help message and exit
-v, --verbose Increase verbosity.
-q, --quiet Decrease verbosity.
-p PYTHON_EXE, --python=PYTHON_EXE
The Python interpreter to use, e.g.,
--python=python2.5 will use the python2.5 interpreter
to create the new environment. The default is the
interpreter that virtualenv was installed with
--clear Clear out the non-root install and start from scratch.
--no-site-packages DEPRECATED. Retained only for backward compatibility.
Not having access to global site-packages is now the
Give the virtual environment access to the global
--always-copy Always copy files rather than symlinking.
--unzip-setuptools Unzip Setuptools when installing it.
--relocatable Make an EXISTING virtualenv environment relocatable.
This fixes up scripts and makes all .pth files
--no-setuptools Do not install setuptools in the new virtualenv.
--no-pip Do not install pip in the new virtualenv.
--no-wheel Do not install wheel in the new virtualenv.
Directory to look for setuptools/pip distributions in.
This option can be used multiple times.
--download Download preinstalled packages from PyPI.
Do not download preinstalled packages from PyPI.
--prompt=PROMPT Provides an alternative prompt prefix for this
--setuptools DEPRECATED. Retained only for backward compatibility.
This option has no effect.
--distribute DEPRECATED. Retained only for backward compatibility.
This option has no effect.
So you’re about to begin a new project. You want to start from scratch and keep it contained to itself. Creating a virtual environment will help you accomplish that:
Now you can go to the newly created directory and see that there are already some folders inside. bin has the executables, include holds the header files, and lib contains the files of the installed modules in the virtual environment.
Before you begin to work with your new environment, you need to activate it with source bin/activate or source env_name/bin/activate if you are outside the environment’s directory.
You will now see your propmt change, to confirm that the environment is active. For me, it looks like:
Now you can begin your work, installing packages, etc. When you’re done, you can type deactivate to exit your environment. If you want to delete it, just remove its directory.
When working with virtual environments, it might be helpful to take a snapshot of your installed packages and their versions, in case you want to recreate the environment later. You can do this with pip freeze > requirements.txt. Check the newly created file for a list of your snapshotted items. In my case, I only installed requests in my environment so far:
If you need the exact same environment later, maybe to let some one of your team to work on the project, you can install the same packages and versions with the command pip install -r requirements.txt
To make working with virtual environments even more convenient, you can use virtualenvwrapper, which provides wrappers for managing your environments and helps keep all your environments in one place. It also allows you to switch between environments with only one command, and has tab completion.
First, install it (outside your environments), with pip install virtualenvwrapper.
Then you need to add a few lines to your shell startup script. In my ~/.bashrc file, I added the following:
# set the location where your virtual environments will be stored
# set the location of the virtualenvwrapper script (where it was installed)
Now reload the startup file: source ~/.bashrc and you will see that your .virtualenvs folder has been created and populated with some scripts. Let’s look at the help menu to see the available commands:
virtualenvwrapper is a set of extensions to Ian Bicking's virtualenv
tool. The extensions include wrappers for creating and deleting
virtual environments and otherwise managing your development workflow,
making it easier to work on more than one project at a time without
introducing conflicts in their dependencies.
For more information please refer to the documentation:
add2virtualenv: add directory to the import path
allvirtualenv: run a command in all virtualenvs
cdproject: change directory to the active project
cdsitepackages: change to the site-packages directory
cdvirtualenv: change to the $VIRTUAL_ENV directory
cpvirtualenv: duplicate the named virtualenv to make a new one
lssitepackages: list contents of the site-packages directory
lsvirtualenv: list virtualenvs
mkproject: create a new project directory and its associated virtualenv
mktmpenv: create a temporary virtualenv
mkvirtualenv: Create a new virtualenv in $WORKON_HOME
rmvirtualenv: Remove a virtualenv
setvirtualenvproject: associate a project directory with a virtualenv
showvirtualenv: show details of a single virtualenv
toggleglobalsitepackages: turn access to global site-packages on/off
virtualenvwrapper: show this help message
wipeenv: remove all packages installed in the current virtualenv
workon: list or change working virtualenvs
Ok, let’s create a new environment: mkvirtualenv beta_proj. You can list all your environments with lsvirtualenv
Use the workon command to begin..working on your new environment: workon beta_proj. When finished, you can use deactivate again,or you can switch between multiple environments with workon.
Removing an environment can be done with the rmvirtualenv command
That’s about all the basic tips needed to quick start your use of virtual environments in your Python projects. For more information, you can check the virtualenv and virtualenvwrapper documentation.
/ Do what comes naturally. Seethe and \
\ fume and throw a tantrum. /