12.1. Introduction

Python applications will often use packages and modules that don’t come as a part of the quality library. Applications will sometimes need a selected version of a library, because the appliance may require that a specific bug has been fixed or the appliance could also be written using an obsolete version of the library’s interface.

This means it’s going to not be possible for one Python installation to satisfy the wants of each application. If application A needs version 1.0 of a specific module but application B needs version 2.0, then the wants are in conflict and installing either version 1.0 or 2.0 will leave one application unable to run.

The solution for this problem is to make a virtual environment, a self-contained directory tree that contains a Python installation for a specific version of Python, plus variety of additional packages.

Different applications can then use different virtual environments. To resolve the sooner example of conflicting requirements, application A can have its own virtual environment with version 1.0 installed while application B has another virtual environment with version 2.0. If application B requires a library be upgraded to version 3.0, this may not affect application A’s environment.

12.2. Creating Virtual Environments

The module wont to create and manage virtual environments is named venv. venv will usually install the foremost recent version of Python that you simply have available. If you’ve got multiple versions of Python on your system, you’ll select a selected Python version by running python3 or whichever version you would like .

To create a virtual environment, decide upon a directory where you would like to put it, and run the venv module as a script with the directory path:

python3 -m venv tutorial-env

This will create the tutorial-env directory if it doesn’t exist, and also create directories inside it containing a replica of the Python interpreter, the quality library, and various supporting files.

A common directory location for a virtual environment is .venv. This name keeps the directory typically hidden in your shell and thus out of the way while giving it a reputation that explains why the directory exists. It also prevents clashing with .env environment variable definition files that some tooling supports.

Once you’ve created a virtual environment, you’ll activate it.

On Windows, run:

tutorial-env\Scripts\activate.bat

On Unix or MacOS, run:

source tutorial-env/bin/activate

(This script is written for the bash shell. If you employ the csh or fish shells, there are alternate activate.csh and activate.fish scripts you ought to use instead.)

Activating the virtual environment will change your shell’s prompt to point out what virtual environment you’re using, and modify the environment in order that running python will get you that specific version and installation of Python. For example:

$ 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/python35.zip’, …,

‘~/envs/tutorial-env/lib/python3.5/site-packages’]

>>>

12.3. Managing Packages with pip

You can install, upgrade, and take away packages employing a program called pip. By default pip will install packages from the Python Package Index, . you’ll browse the Python Package Index by getting to it in your browser , otherwise you can use pip’s limited search feature:

(tutorial-env) $ pip search astronomy

skyfield – Elegant astronomy for Python

gary – Galactic astronomy and gravitational dynamics.

novas – The us Naval Observatory NOVAS astronomy library

astroobs – Provides astronomy ephemeris to plan telescope observations

PyAstronomy – a set of astronomy related tools for Python.

pip features a number of subcommands: “search”, “install”, “uninstall”, “freeze”, etc. (Consult the Installing Python Modules guide for complete documentation for pip.)

You can install the newest version of a package by specifying a package’s name:

(tutorial-env) $ pip install novas

Collecting novas

Downloading novas-3.1.1.3.tar.gz (136kB)

Installing collected packages: novas

Running setup.py install for novas

Successfully installed novas-3.1.1.3

You can also install a selected version of a package by giving the package name followed by == and therefore the version number:

(tutorial-env) $ pip install requests==2.6.0

Collecting requests==2.6.0

Using cached requests-2.6.0-py2.py3-none-any.whl

Installing collected packages: requests

Successfully installed requests-2.6.0

If you re-run this command, pip will notice that the requested version is already installed and do nothing. you’ll supply a special version number to urge that version, otherwise you can run pip install –upgrade to upgrade the package to the newest version:

(tutorial-env) $ pip install –upgrade requests

Collecting requests

Installing collected packages: requests

Found existing installation: requests 2.6.0

Uninstalling requests-2.6.0:

Successfully uninstalled requests-2.6.0

Successfully installed requests-2.7.0

pip uninstall followed by one or more package names will remove the packages from the virtual environment.

pip show will display information a few particular package:

(tutorial-env) $ pip show requests

Metadata-Version: 2.0

Name: requests

Version: 2.7.0

Summary: Python HTTP for Humans.

Home-page: http://python-requests.org

Author: Kenneth Reitz

Author-email: me@kennethreitz.com

License: Apache 2.0

Location: /Users/akuchling/envs/tutorial-env/lib/python3.4/site-packages

Requires:

pip list will display all of the packages installed within the virtual environment:

(tutorial-env) $ pip list

novas (3.1.1.3)

numpy (1.9.2)

pip (7.0.3)

requests (2.7.0)

setuptools (16.0)

pip freeze will produce an identical list of the installed packages, but the output uses the format that pip install expects. a standard convention is to place this list during a requirements.txt file:

(tutorial-env) $ pip freeze > requirements.txt

(tutorial-env) $ cat requirements.txt

novas==3.1.1.3

numpy==1.9.2

requests==2.7.0

The requirements.txt can then be committed to version control and shipped as a part of an application. Users can then install all the required packages with install -r:

(tutorial-env) $ pip install -r requirements.txt

Collecting novas==3.1.1.3 (from -r requirements.txt (line 1))

Collecting numpy==1.9.2 (from -r requirements.txt (line 2))

Collecting requests==2.7.0 (from -r requirements.txt (line 3))

Installing collected packages: novas, numpy, requests

Running setup.py install for novas

Successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0

T