blob: 3b58cae68fcb3e2c194223aee8f384c01821ce06 [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
Vinay Sajipcd9b7462012-07-09 10:37:01 +01009Creation of :ref:`virtual environments <venv-def>` is done by executing the
10``pyvenv`` script::
Vinay Sajip4126a7d2012-05-29 12:52:14 +010011
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
Vinay Sajipcd9b7462012-07-09 10:37:01 +010075 C:\> <venv>/Scripts/activate
Vinay Sajipa945ad12012-07-09 09:24:59 +010076
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