blob: 08fcfd6aeb53f5c958ed7e8d315f9866fb597480 [file] [log] [blame]
Vinay Sajip4126a7d2012-05-29 12:52:14 +01001.. _tools-and-scripts:
2
3Additional Tools and Scripts
4============================
5
6pyvenv - Creating virtual environments
7--------------------------------------
8
9Creation of virtual environments is done by executing the ``pyvenv``
10script::
11
12 pyvenv /path/to/new/virtual/environment
13
14Running this command creates the target directory (creating any parent
15directories that don't exist already) and places a ``pyvenv.cfg`` file
16in it with a ``home`` key pointing to the Python installation the
17command was run from. It also creates a ``bin`` (or ``Scripts`` on
18Windows) subdirectory containing a copy of the ``python`` binary (or
Éric Araujo859aad62012-06-24 00:07:41 -040019binaries, in the case of Windows).
Vinay Sajip4126a7d2012-05-29 12:52:14 +010020It also creates an (initially empty) ``lib/pythonX.Y/site-packages``
21subdirectory (on Windows, this is ``Lib\site-packages``).
22
23.. highlight:: none
24
25On Windows, you may have to invoke the ``pyvenv`` script as follows, if you
26don't have the relevant PATH and PATHEXT settings::
27
28 c:\Temp>c:\Python33\python c:\Python33\Tools\Scripts\pyvenv.py myenv
29
30or equivalently::
31
32 c:\Temp>c:\Python33\python -m venv myenv
33
34The command, if run with ``-h``, will show the available options::
35
Vinay Sajipa945ad12012-07-09 09:24:59 +010036 usage: pyvenv [-h] [--system-site-packages] [--symlinks] [--clear]
Vinay Sajip4126a7d2012-05-29 12:52:14 +010037 [--upgrade] ENV_DIR [ENV_DIR ...]
38
39 Creates virtual Python environments in one or more target directories.
40
41 positional arguments:
42 ENV_DIR A directory to create the environment in.
43
44 optional arguments:
45 -h, --help show this help message and exit
46 --system-site-packages Give access to the global site-packages dir to the
47 virtual environment.
Vinay Sajipa945ad12012-07-09 09:24:59 +010048 --symlinks Try to use symlinks rather than copies, when symlinks
49 are not the default for the platform.
Vinay Sajip4126a7d2012-05-29 12:52:14 +010050 --clear Delete the environment directory if it already exists.
51 If not specified and the directory exists, an error is
52 raised.
53 --upgrade Upgrade the environment directory to use this version
54 of Python, assuming Python has been upgraded in-place.
55
56If the target directory already exists an error will be raised, unless
57the ``--clear`` or ``--upgrade`` option was provided.
58
59The created ``pyvenv.cfg`` file also includes the
60``include-system-site-packages`` key, set to ``true`` if ``venv`` is
61run with the ``--system-site-packages`` option, ``false`` otherwise.
62
63Multiple paths can be given to ``pyvenv``, in which case an identical
64virtualenv will be created, according to the given options, at each
65provided path.
66
Vinay Sajipa945ad12012-07-09 09:24:59 +010067Once a venv has been created, it can be "activated" using a script in the
68venv's binary directory. The invocation of the script is platform-specific: on
69a Posix platform, you would typically do::
70
71 $ source <venv>/bin/activate
72
73whereas on Windows, you might do::
74
75 c:\> <venv>/Scripts/activate
76
77if you are using the ``cmd.exe`` shell, or perhaps::
78
79 PS C:\> <venv>/Scripts/Activate.ps1
80
81if you use PowerShell.
82
83You don't specifically *need* to activate an environment; activation just
84prepends the venv's binary directory to your path, so that "python" invokes the
85venv's Python interpreter and you can run installed scripts without having to
86use their full path. However, all scripts installed in a venv should be
87runnable without activating it, and run with the venv's Python automatically.
88
89You can deactivate a venv by typing "deactivate" in your shell. The exact
90mechanism is platform-specific: for example, the Bash activation script defines
91a "deactivate" function, whereas on Windows there are separate scripts called
92``deactivate.bat`` and ``Deactivate.ps1`` which are installed when the venv is
93created.
94
Vinay Sajip3575f912012-07-03 17:26:55 +010095.. note:: A virtual environment (also called a ``venv``) is a Python
96 environment such that the Python interpreter, libraries and scripts
97 installed into it are isolated from those installed in other virtual
98 environments, and (by default) any libraries installed in a "system" Python,
99 i.e. one which is installed as part of your operating system.
100
101 A venv is a directory tree which contains Python executable files and
102 other files which indicate that it is a venv.
103
104 Common installation tools such as ``distribute`` and ``pip`` work as
105 expected with venvs - i.e. when a venv is active, they install Python
106 packages into the venv without needing to be told to do so explicitly.
107
108 When a venv is active (i.e. the venv's Python interpreter is running), the
109 attributes :attr:`sys.prefix` and :attr:`sys.exec_prefix` point to the base
110 directory of the venv, whereas :attr:`sys.base_prefix` and
111 :attr:`sys.base_exec_prefix` point to the non-venv Python installation
112 which was used to create the venv. If a venv is not active, then
113 :attr:`sys.prefix` is the same as :attr:`sys.base_prefix` and
114 :attr:`sys.exec_prefix` is the same as :attr:`sys.base_exec_prefix` (they
115 all point to a non-venv Python installation).
116