4
*********************************
5
Virtual Environments and Packages
6
*********************************
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.
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.
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.
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.
36
Creating Virtual Environments
37
=============================
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.
46
To create a virtualenv, decide upon a directory
47
where you want to place it and run :program:`pyvenv` with the
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
54
interpreter, the standard library, and various supporting files.
56
Once you've created a virtual environment, you need to
61
tutorial-env/Scripts/activate
63
On Unix or MacOS, run::
65
source tutorial-env/bin/activate
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
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
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)
83
['', '/usr/local/lib/python34.zip', ...,
84
'~/envs/tutorial-env/lib/python3.4/site-packages']
88
Managing Packages with pip
89
==========================
91
Once you've activated a virtual environment, you can install, upgrade,
92
and remove packages using a program called :program:`pip`. By default
93
``pip`` will install packages from the Python Package Index,
94
<https://pypi.python.org/pypi>. You can browse the Python Package Index
95
by going to it in your web browser, or you can use ``pip``'s
96
limited search feature::
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.
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``.)
110
You can install the latest version of a package by specifying a package's name::
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
119
You can also install a specific version of a package by giving the
120
package name followed by ``==`` and the version number::
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
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::
133
-> pip install --upgrade 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
141
``pip uninstall`` followed by one or more package names will remove the
142
packages from the virtual environment.
144
``pip show`` will display information about a particular package::
146
(tutorial-env) -> pip show requests
148
Metadata-Version: 2.0
151
Summary: Python HTTP for Humans.
152
Home-page: http://python-requests.org
153
Author: Kenneth Reitz
154
Author-email: me@kennethreitz.com
156
Location: /Users/akuchling/envs/tutorial-env/lib/python3.4/site-packages
159
``pip list`` will display all of the packages installed in the virtual
162
(tutorial-env) -> pip list
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::
173
(tutorial-env) -> pip freeze > requirements.txt
174
(tutorial-env) -> cat requirements.txt
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``::
183
-> pip install -r requirements.txt
184
Collecting novas==3.1.1.3 (from -r requirements.txt (line 1))
186
Collecting numpy==1.9.2 (from -r requirements.txt (line 2))
188
Collecting requests==2.7.0 (from -r requirements.txt (line 3))
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
194
``pip`` has many more options. Consult the :ref:`installing-index`
195
guide for complete documentation for ``pip``. When you've written
196
a package and want to make it available on the Python Package Index,
197
consult the :ref:`distributing-index` guide.