Some Python Examples
← prev | next →     Top-level ToC     /virtual-envs.html     (printable version)

As of Python 3.5, the venv module is in the standard library. However, to make it easier to use, install the pyvenv convenience script:

apt install python3-venv

That will also ensure that you have pip available in your venvs (though it will not give you a global pip).

I use the term “venv” here to mean: (1) the venv module, and (2) shorthand for “virtual environment”.

Work with venvs like so:

$ pyvenv foo-proj  # Makes the ./foo-proj directory for you.
$ cd foo-proj
# In ./bin, we have `pip{,3,3.5}`, `python{,3,3.5}`  --- all are Python 3 versions.
$ source bin/activate
# Prompt has changed.
(foo-proj)...$ which pip
#=> /path/to/foo-proj/bin/pip
# {work, work, work}
(foo-proj)...$ which python
#=> /path/to/foo-proj/bin/python
(foo-proj)...$ python -V  # This is on Debian Testing.
#=> Python 3.5.1+
(foo-proj)...$ pip install SomeThing      # Installs into this venv's tree.
(foo-proj)...$ pip install --upgrade SomeOutdatedThing
(foo-proj)...$ pip install --upgrade pip  # Ugrade this venv's pip.
(foo-proj)...$ deactivate
# prompt now back to normal

For installing system-wide modules, use apt when possible. If a Python module you want is not available via apt, you might install it using a system-wide pip (which you can get via apt install python3-pip). Using that system-wide pip would install modules under /usr/local (into your global site-packages directory).

Unrelated or don’t-use:

  • virtualenv: OLD (Python 2). Subsumed by venv module in Python 3.
  • Legacy: easy_install,, eggs, Distribute, distutils2,
  • pyenv: This is not pyvenv.

Note, you don’t have to stay or work in the venv directory. Though, if you’ve created a venv for a particular project, it may make sense to keep your project files within that directory.