Virtual Environments

A virtual environment is a light-weight, self-contained Python installation. The main motivation for virtual environments is to allow different projects to have control over the versions of installed Python packages, without interfering with other Python projects installed on the same host. A virtual environment consists of a directory containing a symbolic link to (Unix), or a copy of (Windows), an existing Python installation, together with an empty site-packages directory into which Python packages specific to this virtual
environment can be installed. A second motivation for virtual environments is that users can create a virtual environment without needing administrator rights on their system, making it easy for them to install packages locally. A third motivation is that different virtual environments can be based on different versions of Python, making it easier to test code on say Python 3.4 and Python 3.5 on the same computer.

If you're using Python 3.3 or later, then you should already have a module called venv installed on your system. You can verify this by running it from the command line:

$ python3 -m venv
usage: venv [-h] [--system-site-packages] [--symlinks | --copies]
[--clear]
[--upgrade] [--without-pip]
ENV_DIR [ENV_DIR ...]
venv: error: the following arguments are required: ENV_DIR

If you don't have venv installed, there is another tool

  • The virtualenv environment variable - which works very similarly. You can get it from the Python Package Index (PyPI). We explain how to install packages from PyPI in Appendix C, Installing Third-Party Packages. You can use either venv or virtualenv, though we'll use venv here, since it is built in to recent versions of Python.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset