Link Search Menu Expand Document

Environments in Python

Python programs often use packages and components that are not included in the standard library. Applications will repeatedly need a new version of a library since the application will require that a particular error has been patched or because an outdated version of the library interface will be used to write the application.

It may not be possible for one installation of Python to satisfy each application’s specifications. If application A requires version 1.0 of a particular module, application B requires version 2.0, so there is a dispute between the specifications. If only version 1.0 or 2.0 is installed, one application will be unable to run.

Creating a virtual world, a self-contained directory tree that includes a Python installation for a specific Python version, plus various additional packages, is the solution to this issue.

Various systems will then use multiple virtual worlds. Application A will have its virtual environment with version 1.0 installed to address the previous case of conflicting specifications, while application B has another virtual environment with version 2.0.0. If application B allows the library to be updated to version 3.0, this does not change application A’s environment.

Create Virtual Environments

The module that is used for building and controlling virtual worlds is called venv. Venv will typically update the most current version of Python available to the user. User may choose a particular version of Python by running python3 or whichever version user determines if user have different versions of Python on the system.

To build a virtual environment, determine where to put a directory, and run the venv module as a script with the path to the directory:

python3 -m venv tutorial-env

If it does not exist, this would create the tutorial-env directory and create folders within it containing a replica of the Python paraphraser, and other files supporting it.

A standard directory location for a virtual world is .venv. Typically, this name holds the directory locked in the shell and thus out of the way by giving it a name that explains why there is a directory. It also avoids clashing with .env environment variable specification files that are supported by particular software.

Activating the virtual environment will adjust the shell’s prompt to display which virtual environment the user is using and alter the environment so that the specific version and installation of Python is accessible to the user to run Python. For instance:

$ source ~/envs/tutorial-env/bin/activate
(tutorial-env) $ python
Python 3.5.1 (default, May  6 2016, 10:59:36)
>>> import sys
>>> sys.path
['', '/usr/local/lib/', ...,

Packages Managing with PIP

Using a software named pip, user may load, update, and uninstall packages. By default, pip installs the packages from the Python Index, <>. It redirects to the web browser, user may browse the Python Package Index, or use pip’s narrow search feature:

There are several subcommands in pip: “search,” “install,” “uninstall,” “freeze,” etc. (Consult the Installing Python Modules guide for complete documentation for pip.)

$ python -m pip install novas
Collecting novas
  Downloading novas- (136kB)
Installing collected packages: novas
  Running install for novas

Pip will find that the requested version is already installed and will do nothing if user re-run this order. To get the update, user can have a different version number or run pip install —upgrade to upgrade the software to the new version.

PIP Commands

pip installInstalls the packages in the virtual environment.
pip uninstallRemove the packages from the virtual environment.
pip showDisplays information about a particular package
pip listDisplays all of the packages installed in the virtual environment.
pip freezeProduces a similar list of the installed packages.


Other useful articles:

Back to top

© , Learn Python 101 — All Rights Reserved - Terms of Use - Privacy Policy