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