blob: 89688477b9552cc9dee998987e1649dadbc01c1d [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
36 usage: pyvenv [-h] [--system-site-packages] [--symlink] [--clear]
37 [--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.
48 --symlink Attempt to symlink rather than copy.
49 --clear Delete the environment directory if it already exists.
50 If not specified and the directory exists, an error is
51 raised.
52 --upgrade Upgrade the environment directory to use this version
53 of Python, assuming Python has been upgraded in-place.
54
55If the target directory already exists an error will be raised, unless
56the ``--clear`` or ``--upgrade`` option was provided.
57
58The created ``pyvenv.cfg`` file also includes the
59``include-system-site-packages`` key, set to ``true`` if ``venv`` is
60run with the ``--system-site-packages`` option, ``false`` otherwise.
61
62Multiple paths can be given to ``pyvenv``, in which case an identical
63virtualenv will be created, according to the given options, at each
64provided path.
65
Vinay Sajip3575f912012-07-03 17:26:55 +010066.. note:: A virtual environment (also called a ``venv``) is a Python
67 environment such that the Python interpreter, libraries and scripts
68 installed into it are isolated from those installed in other virtual
69 environments, and (by default) any libraries installed in a "system" Python,
70 i.e. one which is installed as part of your operating system.
71
72 A venv is a directory tree which contains Python executable files and
73 other files which indicate that it is a venv.
74
75 Common installation tools such as ``distribute`` and ``pip`` work as
76 expected with venvs - i.e. when a venv is active, they install Python
77 packages into the venv without needing to be told to do so explicitly.
78
79 When a venv is active (i.e. the venv's Python interpreter is running), the
80 attributes :attr:`sys.prefix` and :attr:`sys.exec_prefix` point to the base
81 directory of the venv, whereas :attr:`sys.base_prefix` and
82 :attr:`sys.base_exec_prefix` point to the non-venv Python installation
83 which was used to create the venv. If a venv is not active, then
84 :attr:`sys.prefix` is the same as :attr:`sys.base_prefix` and
85 :attr:`sys.exec_prefix` is the same as :attr:`sys.base_exec_prefix` (they
86 all point to a non-venv Python installation).
87