|
| 1 | +# 12. Virtual Environments and Packages |
| 2 | + |
| 3 | +### 12.1. Introduction |
| 4 | + |
| 5 | +Python applications will often use packages and modules that don’t come as part of the standard library. Applications will sometimes need a specific version of a library, because the application may require that a particular bug has been fixed or the application may be written using an obsolete version of the library’s interface. |
| 6 | + |
| 7 | +This means it may not be possible for one Python installation to meet the requirements of every application. If application A needs version 1.0 of a particular module but application B needs version 2.0, then the requirements are in conflict and installing either version 1.0 or 2.0 will leave one application unable to run. |
| 8 | + |
| 9 | +The solution for this problem is to create a [virtual environment](https://docs.python.org/3/glossary.html#term-virtual-environment), a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages. |
| 10 | + |
| 11 | +Different applications can then use different virtual environments. To resolve the earlier 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 will not affect application A’s environment. |
| 12 | + |
| 13 | +### 12.2. Creating Virtual Environments |
| 14 | + |
| 15 | +The module used to create and manage virtual environments is called [`venv`](https://docs.python.org/3/library/venv.html#module-venv). [`venv`](https://docs.python.org/3/library/venv.html#module-venv) will usually install the most recent version of Python that you have available. If you have multiple versions of Python on your system, you can select a specific Python version by running `python3` or whichever version you want. |
| 16 | + |
| 17 | +To create a virtual environment, decide upon a directory where you want to place it, and run the [`venv`](https://docs.python.org/3/library/venv.html#module-venv) module as a script with the directory path: |
| 18 | + |
| 19 | +```text |
| 20 | +python3 -m venv tutorial-env |
| 21 | +``` |
| 22 | + |
| 23 | +This will create the `tutorial-env` directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files. |
| 24 | + |
| 25 | +Once you’ve created a virtual environment, you may activate it. |
| 26 | + |
| 27 | +On Windows, run: |
| 28 | + |
| 29 | +```text |
| 30 | +tutorial-env\Scripts\activate.bat |
| 31 | +``` |
| 32 | + |
| 33 | +On Unix or MacOS, run: |
| 34 | + |
| 35 | +```text |
| 36 | +source tutorial-env/bin/activate |
| 37 | +``` |
| 38 | + |
| 39 | +\(This script is written for the bash shell. If you use the **csh** or **fish** shells, there are alternate `activate.csh` and `activate.fish` scripts you should use instead.\) |
| 40 | + |
| 41 | +Activating the virtual environment will change your shell’s prompt to show what virtual environment you’re using, and modify the environment so that running `python` will get you that particular version and installation of Python. For example: |
| 42 | + |
| 43 | +```text |
| 44 | +$ source ~/envs/tutorial-env/bin/activate |
| 45 | +(tutorial-env) $ python |
| 46 | +Python 3.5.1 (default, May 6 2016, 10:59:36) |
| 47 | + ... |
| 48 | +>>> import sys |
| 49 | +>>> sys.path |
| 50 | +['', '/usr/local/lib/python35.zip', ..., |
| 51 | +'~/envs/tutorial-env/lib/python3.5/site-packages'] |
| 52 | +>>> |
| 53 | +``` |
| 54 | + |
| 55 | +### 12.3. Managing Packages with pip |
| 56 | + |
| 57 | +You can install, upgrade, and remove packages using a program called **pip**. By default `pip` will install packages from the Python Package Index, <[https://pypi.python.org/pypi](https://pypi.python.org/pypi)>. You can browse the Python Package Index by going to it in your web browser, or you can use `pip`’s limited search feature: |
| 58 | + |
| 59 | +```text |
| 60 | +(tutorial-env) $ pip search astronomy |
| 61 | +skyfield - Elegant astronomy for Python |
| 62 | +gary - Galactic astronomy and gravitational dynamics. |
| 63 | +novas - The United States Naval Observatory NOVAS astronomy library |
| 64 | +astroobs - Provides astronomy ephemeris to plan telescope observations |
| 65 | +PyAstronomy - A collection of astronomy related tools for Python. |
| 66 | +... |
| 67 | +``` |
| 68 | + |
| 69 | +`pip` has a number of subcommands: “search”, “install”, “uninstall”, “freeze”, etc. \(Consult the [Installing Python Modules](https://docs.python.org/3/installing/index.html#installing-index) guide for complete documentation for `pip`.\) |
| 70 | + |
| 71 | +You can install the latest version of a package by specifying a package’s name: |
| 72 | + |
| 73 | +```text |
| 74 | +(tutorial-env) $ pip install novas |
| 75 | +Collecting novas |
| 76 | + Downloading novas-3.1.1.3.tar.gz (136kB) |
| 77 | +Installing collected packages: novas |
| 78 | + Running setup.py install for novas |
| 79 | +Successfully installed novas-3.1.1.3 |
| 80 | +``` |
| 81 | + |
| 82 | +You can also install a specific version of a package by giving the package name followed by `==` and the version number: |
| 83 | + |
| 84 | +```text |
| 85 | +(tutorial-env) $ pip install requests==2.6.0 |
| 86 | +Collecting requests==2.6.0 |
| 87 | + Using cached requests-2.6.0-py2.py3-none-any.whl |
| 88 | +Installing collected packages: requests |
| 89 | +Successfully installed requests-2.6.0 |
| 90 | +``` |
| 91 | + |
| 92 | +If you re-run this command, `pip` will notice that the requested version is already installed and do nothing. You can supply a different version number to get that version, or you can run `pip install --upgrade` to upgrade the package to the latest version: |
| 93 | + |
| 94 | +```text |
| 95 | +(tutorial-env) $ pip install --upgrade requests |
| 96 | +Collecting requests |
| 97 | +Installing collected packages: requests |
| 98 | + Found existing installation: requests 2.6.0 |
| 99 | + Uninstalling requests-2.6.0: |
| 100 | + Successfully uninstalled requests-2.6.0 |
| 101 | +Successfully installed requests-2.7.0 |
| 102 | +``` |
| 103 | + |
| 104 | +`pip uninstall` followed by one or more package names will remove the packages from the virtual environment. |
| 105 | + |
| 106 | +`pip show` will display information about a particular package: |
| 107 | + |
| 108 | +```text |
| 109 | +(tutorial-env) $ pip show requests |
| 110 | +--- |
| 111 | +Metadata-Version: 2.0 |
| 112 | +Name: requests |
| 113 | +Version: 2.7.0 |
| 114 | +Summary: Python HTTP for Humans. |
| 115 | +Home-page: http://python-requests.org |
| 116 | +Author: Kenneth Reitz |
| 117 | + |
| 118 | +License: Apache 2.0 |
| 119 | +Location: /Users/akuchling/envs/tutorial-env/lib/python3.4/site-packages |
| 120 | +Requires: |
| 121 | +``` |
| 122 | + |
| 123 | +`pip list` will display all of the packages installed in the virtual environment: |
| 124 | + |
| 125 | +```text |
| 126 | +(tutorial-env) $ pip list |
| 127 | +novas (3.1.1.3) |
| 128 | +numpy (1.9.2) |
| 129 | +pip (7.0.3) |
| 130 | +requests (2.7.0) |
| 131 | +setuptools (16.0) |
| 132 | +``` |
| 133 | + |
| 134 | +`pip freeze` will produce a similar list of the installed packages, but the output uses the format that `pip install`expects. A common convention is to put this list in a `requirements.txt` file: |
| 135 | + |
| 136 | +```text |
| 137 | +(tutorial-env) $ pip freeze > requirements.txt |
| 138 | +(tutorial-env) $ cat requirements.txt |
| 139 | +novas==3.1.1.3 |
| 140 | +numpy==1.9.2 |
| 141 | +requests==2.7.0 |
| 142 | +``` |
| 143 | + |
| 144 | +The `requirements.txt` can then be committed to version control and shipped as part of an application. Users can then install all the necessary packages with `install -r`: |
| 145 | + |
| 146 | +```text |
| 147 | +(tutorial-env) $ pip install -r requirements.txt |
| 148 | +Collecting novas==3.1.1.3 (from -r requirements.txt (line 1)) |
| 149 | + ... |
| 150 | +Collecting numpy==1.9.2 (from -r requirements.txt (line 2)) |
| 151 | + ... |
| 152 | +Collecting requests==2.7.0 (from -r requirements.txt (line 3)) |
| 153 | + ... |
| 154 | +Installing collected packages: novas, numpy, requests |
| 155 | + Running setup.py install for novas |
| 156 | +Successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0 |
| 157 | +``` |
| 158 | + |
| 159 | +`pip` has many more options. Consult the [Installing Python Modules](https://docs.python.org/3/installing/index.html#installing-index) guide for complete documentation for `pip`. When you’ve written a package and want to make it available on the Python Package Index, consult the [Distributing Python Modules](https://docs.python.org/3/distributing/index.html#distributing-index) guide. |
| 160 | + |
0 commit comments