blob: 3b2ee2e24c8da4c4567327a4959d93afc8cdb700 [file] [log] [blame]
Andrew Kuchlingdd15b362015-06-08 17:35:45 -04001
2.. _tut-venv:
3
4*********************************
5Virtual Environments and Packages
6*********************************
7
8Introduction
9============
10
11Python applications will often use packages and modules that don't
12come as part of the standard library. Applications will sometimes
13need a specific version of a library, because the application may
14require that a particular bug has been fixed or the application may be
15written using an obsolete version of the library's interface.
16
17This means it may not be possible for one Python installation to meet
18the requirements of every application. If application A needs version
191.0 of a particular module but application B needs version 2.0, then
20the requirements are in conflict and installing either version 1.0 or 2.0
21will leave one application unable to run.
22
23The solution for this problem is to create a :term:`virtual
24environment` (often shortened to "virtualenv"), a self-contained
25directory tree that contains a Python installation for a particular
26version of Python, plus a number of additional packages.
27
28Different applications can then use different virtual environments.
29To resolve the earlier example of conflicting requirements,
30application A can have its own virtual environment with version 1.0
31installed while application B has another virtualenv with version 2.0.
32If application B requires a library be upgraded to version 3.0, this will
33not affect application A's environment.
34
35
36Creating Virtual Environments
37=============================
38
39The script used to create and manage virtual environments is called
40:program:`pyvenv`. :program:`pyvenv` will usually install the most
41recent version of Python that you have available; the script is also
42installed with a version number, so if you have multiple versions of
43Python on your system you can select a specific Python version by
44running ``pyvenv-3.4`` or whichever version you want.
45
46To create a virtualenv, decide upon a directory
47where you want to place it and run :program:`pyvenv` with the
48directory path::
49
50 pyvenv tutorial-env
51
52This will create the ``tutorial-env`` directory if it doesn't exist,
53and also create directories inside it containing a copy of the Python
Andrew Kuchlingd0040712015-06-08 18:17:06 -040054interpreter, the standard library, and various supporting files.
Andrew Kuchlingdd15b362015-06-08 17:35:45 -040055
56Once you've created a virtual environment, you need to
57activate it.
58
59On Windows, run::
60
61 tutorial-env/Scripts/activate
62
63On 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
70instead.)
71
72Activating the virtualenv will change your shell's prompt to show what
73virtualenv you're using, and modify the environment so that running
74``python`` will get you that particular version and installation of
75Python. 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
88Managing Packages with pip
89==========================
90
91Once you've activated a virtual environment, you can install, upgrade,
92and remove packages using a program called :program:`pip`. By default
Tal Einatf330d532015-06-09 18:40:16 +030093``pip`` will install packages from the Python Package Index,
94<https://pypi.python.org/pypi>. You can browse the Python Package Index
Andrew Kuchlingdd15b362015-06-08 17:35:45 -040095by going to it in your web browser, or you can use ``pip``'s
96limited 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
108complete documentation for ``pip``.)
109
110You 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
119You can also install a specific version of a package by giving the
120package 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
128If you re-run this command, ``pip`` will notice that the requested
129version is already installed and do nothing. You can supply a
130different version number to get that version, or you can run ``pip
131install --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
142packages 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
160environment::
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,
170but the output uses the format that ``pip install`` expects.
171A 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
179The ``requirements.txt`` can then be committed to version control and
180shipped as part of an application. Users can then install all the
181necessary 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`
195guide for complete documentation for ``pip``. When you've written
Tal Einatf330d532015-06-09 18:40:16 +0300196a package and want to make it available on the Python Package Index,
Andrew Kuchlingdd15b362015-06-08 17:35:45 -0400197consult the :ref:`distributing-index` guide.