blob: 0873a993c28f9c1e9ed262c6c150cf2a9bd1b156 [file] [log] [blame]
Vinay Sajipc4618e32012-07-10 08:21:07 +01001Creation of :ref:`virtual environments <venv-def>` is done by executing the
2``pyvenv`` script::
3
4 pyvenv /path/to/new/virtual/environment
5
6Running this command creates the target directory (creating any parent
7directories that don't exist already) and places a ``pyvenv.cfg`` file in it
8with a ``home`` key pointing to the Python installation the command was run
9from. It also creates a ``bin`` (or ``Scripts`` on Windows) subdirectory
10containing a copy of the ``python`` binary (or binaries, in the case of
11Windows). It also creates an (initially empty) ``lib/pythonX.Y/site-packages``
12subdirectory (on Windows, this is ``Lib\site-packages``).
13
Larry Hastings3732ed22014-03-15 21:13:56 -070014.. seealso::
15
16 `Python Packaging User Guide: Creating and using virtual environments
Georg Brandl525d3552014-10-29 10:26:56 +010017 <https://packaging.python.org/en/latest/installing.html#virtual-environments>`__
Larry Hastings3732ed22014-03-15 21:13:56 -070018
Vinay Sajipc4618e32012-07-10 08:21:07 +010019.. highlight:: none
20
21On Windows, you may have to invoke the ``pyvenv`` script as follows, if you
22don't have the relevant PATH and PATHEXT settings::
23
Vinay Sajipd0050902015-02-07 10:57:36 +000024 c:\Temp>c:\Python35\python c:\Python35\Tools\Scripts\pyvenv.py myenv
Vinay Sajipc4618e32012-07-10 08:21:07 +010025
26or equivalently::
27
Vinay Sajipd0050902015-02-07 10:57:36 +000028 c:\Temp>c:\Python35\python -m venv myenv
Vinay Sajipc4618e32012-07-10 08:21:07 +010029
30The command, if run with ``-h``, will show the available options::
31
Vinay Sajipc7e34fb2015-02-07 10:52:02 +000032 usage: venv [-h] [--system-site-packages] [--symlinks] [--clear]
33 [--upgrade] [--without-pip] ENV_DIR [ENV_DIR ...]
Vinay Sajipc4618e32012-07-10 08:21:07 +010034
35 Creates virtual Python environments in one or more target directories.
36
37 positional arguments:
38 ENV_DIR A directory to create the environment in.
39
40 optional arguments:
41 -h, --help show this help message and exit
42 --system-site-packages Give access to the global site-packages dir to the
43 virtual environment.
44 --symlinks Try to use symlinks rather than copies, when symlinks
45 are not the default for the platform.
Larry Hastings3732ed22014-03-15 21:13:56 -070046 --copies Try to use copies rather than symlinks, even when
47 symlinks are the default for the platform.
Berker Peksag547f66f2016-01-28 09:01:26 +020048 --clear Delete the contents of the environment directory if it
49 already exists, before environment creation.
Vinay Sajipc4618e32012-07-10 08:21:07 +010050 --upgrade Upgrade the environment directory to use this version
51 of Python, assuming Python has been upgraded in-place.
Nick Coghlan8fbdb092013-11-23 00:30:34 +100052 --without-pip Skips installing or upgrading pip in the virtual
53 environment (pip is bootstrapped by default)
54
Vinay Sajipc7e34fb2015-02-07 10:52:02 +000055Depending on how the ``venv`` functionality has been invoked, the usage message
56may vary slightly, e.g. referencing ``pyvenv`` rather than ``venv``.
57
Nick Coghlan8fbdb092013-11-23 00:30:34 +100058.. versionchanged:: 3.4
Larry Hastings3732ed22014-03-15 21:13:56 -070059 Installs pip by default, added the ``--without-pip`` and ``--copies``
60 options
Vinay Sajipc4618e32012-07-10 08:21:07 +010061
Vinay Sajip71e72962015-01-23 19:35:12 +000062.. versionchanged:: 3.4
63 In earlier versions, if the target directory already existed, an error was
64 raised, unless the ``--clear`` or ``--upgrade`` option was provided. Now,
65 if an existing directory is specified, its contents are removed and
66 the directory is processed as if it had been newly created.
Vinay Sajipc4618e32012-07-10 08:21:07 +010067
68The created ``pyvenv.cfg`` file also includes the
69``include-system-site-packages`` key, set to ``true`` if ``venv`` is
70run with the ``--system-site-packages`` option, ``false`` otherwise.
71
Nick Coghlan8fbdb092013-11-23 00:30:34 +100072Unless the ``--without-pip`` option is given, :mod:`ensurepip` will be
73invoked to bootstrap ``pip`` into the virtual environment.
74
Vinay Sajipc4618e32012-07-10 08:21:07 +010075Multiple paths can be given to ``pyvenv``, in which case an identical
76virtualenv will be created, according to the given options, at each
77provided path.
78
79Once a venv has been created, it can be "activated" using a script in the
Andrew Svetlov65e9c572012-10-04 21:48:58 +030080venv's binary directory. The invocation of the script is platform-specific:
Vinay Sajipc4618e32012-07-10 08:21:07 +010081
Andrew Svetlov65e9c572012-10-04 21:48:58 +030082+-------------+-----------------+-----------------------------------------+
83| Platform | Shell | Command to activate virtual environment |
84+=============+=================+=========================================+
85| Posix | bash/zsh | $ source <venv>/bin/activate |
86+-------------+-----------------+-----------------------------------------+
87| | fish | $ . <venv>/bin/activate.fish |
88+-------------+-----------------+-----------------------------------------+
89| | csh/tcsh | $ source <venv>/bin/activate.csh |
90+-------------+-----------------+-----------------------------------------+
91| Windows | cmd.exe | C:\> <venv>/Scripts/activate.bat |
92+-------------+-----------------+-----------------------------------------+
93| | PowerShell | PS C:\> <venv>/Scripts/Activate.ps1 |
94+-------------+-----------------+-----------------------------------------+
Vinay Sajipc4618e32012-07-10 08:21:07 +010095
96You don't specifically *need* to activate an environment; activation just
97prepends the venv's binary directory to your path, so that "python" invokes the
98venv's Python interpreter and you can run installed scripts without having to
99use their full path. However, all scripts installed in a venv should be
100runnable without activating it, and run with the venv's Python automatically.
101
102You can deactivate a venv by typing "deactivate" in your shell. The exact
103mechanism is platform-specific: for example, the Bash activation script defines
104a "deactivate" function, whereas on Windows there are separate scripts called
105``deactivate.bat`` and ``Deactivate.ps1`` which are installed when the venv is
106created.
107
R David Murray575fb312013-12-25 23:21:03 -0500108.. versionadded:: 3.4
109 ``fish`` and ``csh`` activation scripts.