blob: 53f431b5cfda6c661efab334ba87848c24eb0ff5 [file] [log] [blame]
Vinay Sajipc4618e32012-07-10 08:21:07 +01001Creation of :ref:`virtual environments <venv-def>` is done by executing the
Brett Cannon15552c32016-07-08 10:46:21 -07002command ``venv``::
Vinay Sajipc4618e32012-07-10 08:21:07 +01003
Brett Cannon15552c32016-07-08 10:46:21 -07004 python3 -m venv /path/to/new/virtual/environment
Vinay Sajipc4618e32012-07-10 08:21:07 +01005
6Running this command creates the target directory (creating any parent
7directories that don't exist already) and places a ``pyvenv.cfg`` file in it
Brett Cannon15552c32016-07-08 10:46:21 -07008with a ``home`` key pointing to the Python installation from which the command
9was run. It also creates a ``bin`` (or ``Scripts`` on Windows) subdirectory
Vinay Sajipc4618e32012-07-10 08:21:07 +010010containing 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
Brett Cannon15552c32016-07-08 10:46:21 -070014.. deprecated:: 3.6
15 ``pyvenv`` was the recommended tool for creating virtual environments for
16 Python 3.3 and 3.4, and is `deprecated in Python 3.6
17 <https://docs.python.org/dev/whatsnew/3.6.html#deprecated-features>`_.
18
19.. versionchanged:: 3.5
20 The use of ``venv`` is now recommended for creating virtual environments.
21
Larry Hastings3732ed22014-03-15 21:13:56 -070022.. seealso::
23
24 `Python Packaging User Guide: Creating and using virtual environments
Brett Cannon15552c32016-07-08 10:46:21 -070025 <https://packaging.python.org/installing/#creating-virtual-environments>`__
Larry Hastings3732ed22014-03-15 21:13:56 -070026
Vinay Sajipc4618e32012-07-10 08:21:07 +010027.. highlight:: none
28
Brett Cannon15552c32016-07-08 10:46:21 -070029On Windows, invoke the ``venv`` command as follows::
Vinay Sajipc4618e32012-07-10 08:21:07 +010030
Brett Cannon15552c32016-07-08 10:46:21 -070031 c:\>c:\Python35\python -m venv c:\path\to\myenv
Vinay Sajipc4618e32012-07-10 08:21:07 +010032
Brett Cannon15552c32016-07-08 10:46:21 -070033Alternatively, if you configured the ``PATH`` and ``PATHEXT`` variables for
34your :ref:`Python installation <using-on-windows>`::
Vinay Sajipc4618e32012-07-10 08:21:07 +010035
Brett Cannon15552c32016-07-08 10:46:21 -070036 c:\>python -m venv myenv c:\path\to\myenv
Vinay Sajipc4618e32012-07-10 08:21:07 +010037
38The command, if run with ``-h``, will show the available options::
39
Berker Peksaga9744ae2016-01-30 12:17:10 +020040 usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
41 [--upgrade] [--without-pip]
42 ENV_DIR [ENV_DIR ...]
Vinay Sajipc4618e32012-07-10 08:21:07 +010043
44 Creates virtual Python environments in one or more target directories.
45
46 positional arguments:
Brett Cannon15552c32016-07-08 10:46:21 -070047 ENV_DIR A directory to create the environment in.
Vinay Sajipc4618e32012-07-10 08:21:07 +010048
49 optional arguments:
Brett Cannon15552c32016-07-08 10:46:21 -070050 -h, --help show this help message and exit
51 --system-site-packages
52 Give the virtual environment access to the system
53 site-packages dir.
54 --symlinks Try to use symlinks rather than copies, when symlinks
55 are not the default for the platform.
56 --copies Try to use copies rather than symlinks, even when
57 symlinks are the default for the platform.
58 --clear Delete the contents of the environment directory if it
59 already exists, before environment creation.
60 --upgrade Upgrade the environment directory to use this version
61 of Python, assuming Python has been upgraded in-place.
62 --without-pip Skips installing or upgrading pip in the virtual
63 environment (pip is bootstrapped by default)
Nick Coghlan8fbdb092013-11-23 00:30:34 +100064
Brett Cannon15552c32016-07-08 10:46:21 -070065 Once an environment has been created, you may wish to activate it, e.g. by
66 sourcing an activate script in its bin directory.
Vinay Sajipc7e34fb2015-02-07 10:52:02 +000067
Nick Coghlan8fbdb092013-11-23 00:30:34 +100068.. versionchanged:: 3.4
Larry Hastings3732ed22014-03-15 21:13:56 -070069 Installs pip by default, added the ``--without-pip`` and ``--copies``
70 options
Vinay Sajipc4618e32012-07-10 08:21:07 +010071
Vinay Sajip71e72962015-01-23 19:35:12 +000072.. versionchanged:: 3.4
73 In earlier versions, if the target directory already existed, an error was
74 raised, unless the ``--clear`` or ``--upgrade`` option was provided. Now,
75 if an existing directory is specified, its contents are removed and
76 the directory is processed as if it had been newly created.
Vinay Sajipc4618e32012-07-10 08:21:07 +010077
78The created ``pyvenv.cfg`` file also includes the
79``include-system-site-packages`` key, set to ``true`` if ``venv`` is
80run with the ``--system-site-packages`` option, ``false`` otherwise.
81
Nick Coghlan8fbdb092013-11-23 00:30:34 +100082Unless the ``--without-pip`` option is given, :mod:`ensurepip` will be
83invoked to bootstrap ``pip`` into the virtual environment.
84
Brett Cannon15552c32016-07-08 10:46:21 -070085Multiple paths can be given to ``venv``, in which case an identical virtual
86environment will be created, according to the given options, at each provided
87path.
Vinay Sajipc4618e32012-07-10 08:21:07 +010088
Brett Cannon15552c32016-07-08 10:46:21 -070089Once a virtual environment has been created, it can be "activated" using a
90script in the virtual environment's binary directory. The invocation of the
91script is platform-specific:
Vinay Sajipc4618e32012-07-10 08:21:07 +010092
Andrew Svetlov65e9c572012-10-04 21:48:58 +030093+-------------+-----------------+-----------------------------------------+
94| Platform | Shell | Command to activate virtual environment |
95+=============+=================+=========================================+
96| Posix | bash/zsh | $ source <venv>/bin/activate |
97+-------------+-----------------+-----------------------------------------+
98| | fish | $ . <venv>/bin/activate.fish |
99+-------------+-----------------+-----------------------------------------+
100| | csh/tcsh | $ source <venv>/bin/activate.csh |
101+-------------+-----------------+-----------------------------------------+
Berker Peksag6803f352016-06-27 13:10:47 +0300102| Windows | cmd.exe | C:\\> <venv>\\Scripts\\activate.bat |
Andrew Svetlov65e9c572012-10-04 21:48:58 +0300103+-------------+-----------------+-----------------------------------------+
Berker Peksag6803f352016-06-27 13:10:47 +0300104| | PowerShell | PS C:\\> <venv>\\Scripts\\Activate.ps1 |
Andrew Svetlov65e9c572012-10-04 21:48:58 +0300105+-------------+-----------------+-----------------------------------------+
Vinay Sajipc4618e32012-07-10 08:21:07 +0100106
107You don't specifically *need* to activate an environment; activation just
Brett Cannon15552c32016-07-08 10:46:21 -0700108prepends the virtual environment's binary directory to your path, so that
109"python" invokes the virtual environment's Python interpreter and you can run
110installed scripts without having to use their full path. However, all scripts
111installed in a virtual environment should be runnable without activating it,
112and run with the virtual environment's Python automatically.
Vinay Sajipc4618e32012-07-10 08:21:07 +0100113
Brett Cannon15552c32016-07-08 10:46:21 -0700114You can deactivate a virtual environment by typing "deactivate" in your shell.
115The exact mechanism is platform-specific: for example, the Bash activation
116script defines a "deactivate" function, whereas on Windows there are separate
117scripts called ``deactivate.bat`` and ``Deactivate.ps1`` which are installed
118when the virtual environment is created.
Vinay Sajipc4618e32012-07-10 08:21:07 +0100119
R David Murray575fb312013-12-25 23:21:03 -0500120.. versionadded:: 3.4
121 ``fish`` and ``csh`` activation scripts.