Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 1 | |
| 2 | .. _tut-venv: |
| 3 | |
| 4 | ********************************* |
| 5 | Virtual Environments and Packages |
| 6 | ********************************* |
| 7 | |
| 8 | Introduction |
| 9 | ============ |
| 10 | |
| 11 | Python applications will often use packages and modules that don't |
| 12 | come as part of the standard library. Applications will sometimes |
| 13 | need a specific version of a library, because the application may |
| 14 | require that a particular bug has been fixed or the application may be |
| 15 | written using an obsolete version of the library's interface. |
| 16 | |
| 17 | This means it may not be possible for one Python installation to meet |
| 18 | the requirements of every application. If application A needs version |
| 19 | 1.0 of a particular module but application B needs version 2.0, then |
| 20 | the requirements are in conflict and installing either version 1.0 or 2.0 |
| 21 | will leave one application unable to run. |
| 22 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 23 | The solution for this problem is to create a :term:`virtual environment`, a |
| 24 | self-contained directory tree that contains a Python installation for a |
| 25 | particular version of Python, plus a number of additional packages. |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 26 | |
| 27 | Different applications can then use different virtual environments. |
| 28 | To resolve the earlier example of conflicting requirements, |
| 29 | application A can have its own virtual environment with version 1.0 |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 30 | installed while application B has another virtual environment with version 2.0. |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 31 | If application B requires a library be upgraded to version 3.0, this will |
| 32 | not affect application A's environment. |
| 33 | |
| 34 | |
| 35 | Creating Virtual Environments |
| 36 | ============================= |
| 37 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 38 | The module used to create and manage virtual environments is called |
| 39 | :mod:`venv`. :mod:`venv` will usually install the most recent version of |
| 40 | Python that you have available. If you have multiple versions of Python on your |
| 41 | system, you can select a specific Python version by running ``python3`` or |
| 42 | whichever version you want. |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 43 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 44 | To create a virtual environment, decide upon a directory where you want to |
| 45 | place it, and run the :mod:`venv` module as a script with the directory path:: |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 46 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 47 | python3 -m venv tutorial-env |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 48 | |
| 49 | This will create the ``tutorial-env`` directory if it doesn't exist, |
| 50 | and also create directories inside it containing a copy of the Python |
Andrew Kuchling | d004071 | 2015-06-08 18:17:06 -0400 | [diff] [blame] | 51 | interpreter, the standard library, and various supporting files. |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 52 | |
Brett Cannon | f9f8e3c | 2019-06-28 12:14:31 -0700 | [diff] [blame] | 53 | A common directory location for a virtual environment is ``.venv``. |
| 54 | This name keeps the directory typically hidden in your shell and thus |
| 55 | out of the way while giving it a name that explains why the directory |
| 56 | exists. It also prevents clashing with ``.env`` environment variable |
| 57 | definition files that some tooling supports. |
| 58 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 59 | Once you've created a virtual environment, you may activate it. |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 60 | |
| 61 | On Windows, run:: |
| 62 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 63 | tutorial-env\Scripts\activate.bat |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 64 | |
| 65 | On Unix or MacOS, run:: |
| 66 | |
| 67 | source tutorial-env/bin/activate |
| 68 | |
| 69 | (This script is written for the bash shell. If you use the |
| 70 | :program:`csh` or :program:`fish` shells, there are alternate |
| 71 | ``activate.csh`` and ``activate.fish`` scripts you should use |
| 72 | instead.) |
| 73 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 74 | Activating the virtual environment will change your shell's prompt to show what |
| 75 | virtual environment you're using, and modify the environment so that running |
| 76 | ``python`` will get you that particular version and installation of Python. |
| 77 | For example: |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 78 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 79 | .. code-block:: bash |
| 80 | |
| 81 | $ source ~/envs/tutorial-env/bin/activate |
| 82 | (tutorial-env) $ python |
| 83 | Python 3.5.1 (default, May 6 2016, 10:59:36) |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 84 | ... |
| 85 | >>> import sys |
| 86 | >>> sys.path |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 87 | ['', '/usr/local/lib/python35.zip', ..., |
| 88 | '~/envs/tutorial-env/lib/python3.5/site-packages'] |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 89 | >>> |
| 90 | |
| 91 | |
| 92 | Managing Packages with pip |
| 93 | ========================== |
| 94 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 95 | You can install, upgrade, and remove packages using a program called |
| 96 | :program:`pip`. By default ``pip`` will install packages from the Python |
Stéphane Wirtel | 19177fb | 2018-05-15 20:58:35 +0200 | [diff] [blame] | 97 | Package Index, <https://pypi.org>. You can browse the Python |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 98 | Package Index by going to it in your web browser, or you can use ``pip``'s |
| 99 | limited search feature: |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 100 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 101 | .. code-block:: bash |
| 102 | |
| 103 | (tutorial-env) $ pip search astronomy |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 104 | skyfield - Elegant astronomy for Python |
| 105 | gary - Galactic astronomy and gravitational dynamics. |
| 106 | novas - The United States Naval Observatory NOVAS astronomy library |
| 107 | astroobs - Provides astronomy ephemeris to plan telescope observations |
| 108 | PyAstronomy - A collection of astronomy related tools for Python. |
| 109 | ... |
| 110 | |
| 111 | ``pip`` has a number of subcommands: "search", "install", "uninstall", |
| 112 | "freeze", etc. (Consult the :ref:`installing-index` guide for |
| 113 | complete documentation for ``pip``.) |
| 114 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 115 | You can install the latest version of a package by specifying a package's name: |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 116 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 117 | .. code-block:: bash |
| 118 | |
Brad | 8b2aa1f | 2019-06-21 14:20:21 -0400 | [diff] [blame] | 119 | (tutorial-env) $ python -m pip install novas |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 120 | Collecting novas |
| 121 | Downloading novas-3.1.1.3.tar.gz (136kB) |
| 122 | Installing collected packages: novas |
| 123 | Running setup.py install for novas |
| 124 | Successfully installed novas-3.1.1.3 |
| 125 | |
| 126 | You can also install a specific version of a package by giving the |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 127 | package name followed by ``==`` and the version number: |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 128 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 129 | .. code-block:: bash |
| 130 | |
Brad | 8b2aa1f | 2019-06-21 14:20:21 -0400 | [diff] [blame] | 131 | (tutorial-env) $ python -m pip install requests==2.6.0 |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 132 | Collecting requests==2.6.0 |
| 133 | Using cached requests-2.6.0-py2.py3-none-any.whl |
| 134 | Installing collected packages: requests |
| 135 | Successfully installed requests-2.6.0 |
| 136 | |
| 137 | If you re-run this command, ``pip`` will notice that the requested |
| 138 | version is already installed and do nothing. You can supply a |
| 139 | different version number to get that version, or you can run ``pip |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 140 | install --upgrade`` to upgrade the package to the latest version: |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 141 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 142 | .. code-block:: bash |
| 143 | |
Brad | 8b2aa1f | 2019-06-21 14:20:21 -0400 | [diff] [blame] | 144 | (tutorial-env) $ python -m pip install --upgrade requests |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 145 | Collecting requests |
| 146 | Installing collected packages: requests |
| 147 | Found existing installation: requests 2.6.0 |
| 148 | Uninstalling requests-2.6.0: |
| 149 | Successfully uninstalled requests-2.6.0 |
| 150 | Successfully installed requests-2.7.0 |
| 151 | |
| 152 | ``pip uninstall`` followed by one or more package names will remove the |
| 153 | packages from the virtual environment. |
| 154 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 155 | ``pip show`` will display information about a particular package: |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 156 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 157 | .. code-block:: bash |
| 158 | |
| 159 | (tutorial-env) $ pip show requests |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 160 | --- |
| 161 | Metadata-Version: 2.0 |
| 162 | Name: requests |
| 163 | Version: 2.7.0 |
| 164 | Summary: Python HTTP for Humans. |
| 165 | Home-page: http://python-requests.org |
| 166 | Author: Kenneth Reitz |
| 167 | Author-email: me@kennethreitz.com |
| 168 | License: Apache 2.0 |
| 169 | Location: /Users/akuchling/envs/tutorial-env/lib/python3.4/site-packages |
| 170 | Requires: |
| 171 | |
| 172 | ``pip list`` will display all of the packages installed in the virtual |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 173 | environment: |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 174 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 175 | .. code-block:: bash |
| 176 | |
| 177 | (tutorial-env) $ pip list |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 178 | novas (3.1.1.3) |
| 179 | numpy (1.9.2) |
| 180 | pip (7.0.3) |
| 181 | requests (2.7.0) |
| 182 | setuptools (16.0) |
| 183 | |
| 184 | ``pip freeze`` will produce a similar list of the installed packages, |
| 185 | but the output uses the format that ``pip install`` expects. |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 186 | A common convention is to put this list in a ``requirements.txt`` file: |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 187 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 188 | .. code-block:: bash |
| 189 | |
| 190 | (tutorial-env) $ pip freeze > requirements.txt |
| 191 | (tutorial-env) $ cat requirements.txt |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 192 | novas==3.1.1.3 |
| 193 | numpy==1.9.2 |
| 194 | requests==2.7.0 |
| 195 | |
| 196 | The ``requirements.txt`` can then be committed to version control and |
| 197 | shipped as part of an application. Users can then install all the |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 198 | necessary packages with ``install -r``: |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 199 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 200 | .. code-block:: bash |
| 201 | |
Brad | 8b2aa1f | 2019-06-21 14:20:21 -0400 | [diff] [blame] | 202 | (tutorial-env) $ python -m pip install -r requirements.txt |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 203 | Collecting novas==3.1.1.3 (from -r requirements.txt (line 1)) |
| 204 | ... |
| 205 | Collecting numpy==1.9.2 (from -r requirements.txt (line 2)) |
| 206 | ... |
| 207 | Collecting requests==2.7.0 (from -r requirements.txt (line 3)) |
| 208 | ... |
| 209 | Installing collected packages: novas, numpy, requests |
| 210 | Running setup.py install for novas |
| 211 | Successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0 |
| 212 | |
| 213 | ``pip`` has many more options. Consult the :ref:`installing-index` |
| 214 | guide for complete documentation for ``pip``. When you've written |
Tal Einat | f330d53 | 2015-06-09 18:40:16 +0300 | [diff] [blame] | 215 | a package and want to make it available on the Python Package Index, |
Andrew Kuchling | dd15b36 | 2015-06-08 17:35:45 -0400 | [diff] [blame] | 216 | consult the :ref:`distributing-index` guide. |