Working with virtualenv
First, we need to make sure that we have the virtualenv
tool installed in our local system. This is a simple matter of fetching it from the PyPI repository:
$ pip install virtualenv
Note
For obvious reasons, this package should be installed outside any virtual environments that may already exist.
Creating a new virtual environment
Creating a new virtual environment is straightforward. The following command will create a new folder at the specified path that will contain the necessary structure and scripts, including a full copy of your default Python binary:
$ virtualenv <path/to/env/directory>
If we want to create an environment that lives at ~/envs/testing
, we will first ensure that the parent directory exists and then invoke the following command:
$ mkdir -p ~/envs $ virtualenv ~/envs/testing
In Python 3.3+, a mostly API-compatible version of the virtualenv
tool was added to the default language packages. The name of the module is venv
, however, the name of the script that allows you to create a virtual environment is pyvenv
and can be invoked in a similar way as the previously discussed virtualenv
tool, as follows:
$ mkdir -p ~/envs $ pyvenv ~/envs/testing
Activating and deactivating virtual environments
Creating a virtual environment does not automatically activate it. Once the environment is created, we need to activate it so that any modifications to the Python environment (for example, installing packages) will occur in the isolated environment instead of our system global one. By default, the activation of a virtual environment will alter the prompt string ($PS1
) of the currently active user so that it displays the name of the sourced virtual environment:
$ source ~/envs/testing/bin/activate (testing) $ # Command prompt modified to display current virtualenv
The command is the same for Python 3.3+:
$ source ~/envs/testing/bin/activate (testing) $ # Command prompt modified to display current virtualenv
When you run the above command, the following series of steps occurs:
- Deactivates any already activated environment.
- Prepends your
$PATH
variable with the location of thevirtualenv bin/
directory, for example,~/envs/testing/bin:$PATH
. - Unsets
$PYTHONHOME
if it exists. - Modifies your interactive shell prompt so that it includes the name of the currently active
virtualenv
.
As a result of the $PATH
environment variable manipulations, the Python and pip
binaries (and whatever other binaries that were installed via pip
), which have been invoked via the shell where the environment was activated, will be the ones contained in ~/envs/testing/bin
.
Adding packages to an existing environment
We can easily add packages to a virtual environment by simply activating it and then invoking pip
in the following way:
$ source ~/envs/testing/bin/activate (testing)$ pip install numpy
This will install the numpy
package to the testing environment, and only the testing environment. Your global system packages will be unaffected, as well as any other existing environments.
Uninstalling packages from an existing environment
Uninstalling a pip
package is straightforward as well:
$ source ~/envs/testing/bin/activate (testing)$ pip uninstall numpy
This will remove the numpy
package from the testing environment only.
Here is one relatively major place where the Python package management falls short: uninstalling a package does not uninstall its dependencies. For example, if you install package A
and it installs dependent packages B
and C
, uninstalling package A
at a later time will not uninstall B
and C
.