blob: ba5096abd370a5b1bdab8f985333a89934b79798 [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
mkkotf5107df2018-12-14 21:28:52 +010010containing a copy/symlink of the Python binary/binaries (as appropriate for the
11platform or arguments used at environment creation time). It also creates an
12(initially empty) ``lib/pythonX.Y/site-packages`` subdirectory
13(on Windows, this is ``Lib\site-packages``). If an existing
TROUVERIE Joachime8eb9722018-02-18 17:52:36 +010014directory is specified, it will be re-used.
Vinay Sajipc4618e32012-07-10 08:21:07 +010015
Brett Cannon15552c32016-07-08 10:46:21 -070016.. deprecated:: 3.6
17 ``pyvenv`` was the recommended tool for creating virtual environments for
18 Python 3.3 and 3.4, and is `deprecated in Python 3.6
19 <https://docs.python.org/dev/whatsnew/3.6.html#deprecated-features>`_.
20
21.. versionchanged:: 3.5
22 The use of ``venv`` is now recommended for creating virtual environments.
23
Vinay Sajipc4618e32012-07-10 08:21:07 +010024.. highlight:: none
25
Brett Cannon15552c32016-07-08 10:46:21 -070026On Windows, invoke the ``venv`` command as follows::
Vinay Sajipc4618e32012-07-10 08:21:07 +010027
Brett Cannon15552c32016-07-08 10:46:21 -070028 c:\>c:\Python35\python -m venv c:\path\to\myenv
Vinay Sajipc4618e32012-07-10 08:21:07 +010029
Brett Cannon15552c32016-07-08 10:46:21 -070030Alternatively, if you configured the ``PATH`` and ``PATHEXT`` variables for
31your :ref:`Python installation <using-on-windows>`::
Vinay Sajipc4618e32012-07-10 08:21:07 +010032
cocoatomod609b0c2017-10-27 13:42:11 +090033 c:\>python -m venv c:\path\to\myenv
Vinay Sajipc4618e32012-07-10 08:21:07 +010034
35The command, if run with ``-h``, will show the available options::
36
Berker Peksaga9744ae2016-01-30 12:17:10 +020037 usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
38 [--upgrade] [--without-pip]
39 ENV_DIR [ENV_DIR ...]
Vinay Sajipc4618e32012-07-10 08:21:07 +010040
41 Creates virtual Python environments in one or more target directories.
42
43 positional arguments:
Brett Cannon15552c32016-07-08 10:46:21 -070044 ENV_DIR A directory to create the environment in.
Vinay Sajipc4618e32012-07-10 08:21:07 +010045
46 optional arguments:
Brett Cannon15552c32016-07-08 10:46:21 -070047 -h, --help show this help message and exit
48 --system-site-packages
49 Give the virtual environment access to the system
50 site-packages dir.
51 --symlinks Try to use symlinks rather than copies, when symlinks
52 are not the default for the platform.
53 --copies Try to use copies rather than symlinks, even when
54 symlinks are the default for the platform.
55 --clear Delete the contents of the environment directory if it
56 already exists, before environment creation.
57 --upgrade Upgrade the environment directory to use this version
58 of Python, assuming Python has been upgraded in-place.
59 --without-pip Skips installing or upgrading pip in the virtual
60 environment (pip is bootstrapped by default)
Nick Coghlan8fbdb092013-11-23 00:30:34 +100061
Brett Cannon15552c32016-07-08 10:46:21 -070062 Once an environment has been created, you may wish to activate it, e.g. by
63 sourcing an activate script in its bin directory.
Vinay Sajipc7e34fb2015-02-07 10:52:02 +000064
Nick Coghlan8fbdb092013-11-23 00:30:34 +100065.. versionchanged:: 3.4
Larry Hastings3732ed22014-03-15 21:13:56 -070066 Installs pip by default, added the ``--without-pip`` and ``--copies``
67 options
Vinay Sajipc4618e32012-07-10 08:21:07 +010068
Vinay Sajip71e72962015-01-23 19:35:12 +000069.. versionchanged:: 3.4
70 In earlier versions, if the target directory already existed, an error was
TROUVERIE Joachime8eb9722018-02-18 17:52:36 +010071 raised, unless the ``--clear`` or ``--upgrade`` option was provided.
Vinay Sajipc4618e32012-07-10 08:21:07 +010072
73The created ``pyvenv.cfg`` file also includes the
74``include-system-site-packages`` key, set to ``true`` if ``venv`` is
75run with the ``--system-site-packages`` option, ``false`` otherwise.
76
Nick Coghlan8fbdb092013-11-23 00:30:34 +100077Unless the ``--without-pip`` option is given, :mod:`ensurepip` will be
78invoked to bootstrap ``pip`` into the virtual environment.
79
Brett Cannon15552c32016-07-08 10:46:21 -070080Multiple paths can be given to ``venv``, in which case an identical virtual
81environment will be created, according to the given options, at each provided
82path.
Vinay Sajipc4618e32012-07-10 08:21:07 +010083
Brett Cannon15552c32016-07-08 10:46:21 -070084Once a virtual environment has been created, it can be "activated" using a
85script in the virtual environment's binary directory. The invocation of the
Julien Palardd936a8f2018-11-21 09:40:05 +010086script is platform-specific (`<venv>` must be replaced by the path of the
87directory containing the virtual environment):
Vinay Sajipc4618e32012-07-10 08:21:07 +010088
Andrew Svetlov65e9c572012-10-04 21:48:58 +030089+-------------+-----------------+-----------------------------------------+
90| Platform | Shell | Command to activate virtual environment |
91+=============+=================+=========================================+
92| Posix | bash/zsh | $ source <venv>/bin/activate |
93+-------------+-----------------+-----------------------------------------+
94| | fish | $ . <venv>/bin/activate.fish |
95+-------------+-----------------+-----------------------------------------+
96| | csh/tcsh | $ source <venv>/bin/activate.csh |
97+-------------+-----------------+-----------------------------------------+
Berker Peksag6803f352016-06-27 13:10:47 +030098| Windows | cmd.exe | C:\\> <venv>\\Scripts\\activate.bat |
Andrew Svetlov65e9c572012-10-04 21:48:58 +030099+-------------+-----------------+-----------------------------------------+
Berker Peksag6803f352016-06-27 13:10:47 +0300100| | PowerShell | PS C:\\> <venv>\\Scripts\\Activate.ps1 |
Andrew Svetlov65e9c572012-10-04 21:48:58 +0300101+-------------+-----------------+-----------------------------------------+
Vinay Sajipc4618e32012-07-10 08:21:07 +0100102
103You don't specifically *need* to activate an environment; activation just
Brett Cannon15552c32016-07-08 10:46:21 -0700104prepends the virtual environment's binary directory to your path, so that
105"python" invokes the virtual environment's Python interpreter and you can run
106installed scripts without having to use their full path. However, all scripts
107installed in a virtual environment should be runnable without activating it,
108and run with the virtual environment's Python automatically.
Vinay Sajipc4618e32012-07-10 08:21:07 +0100109
Brett Cannon15552c32016-07-08 10:46:21 -0700110You can deactivate a virtual environment by typing "deactivate" in your shell.
111The exact mechanism is platform-specific: for example, the Bash activation
112script defines a "deactivate" function, whereas on Windows there are separate
113scripts called ``deactivate.bat`` and ``Deactivate.ps1`` which are installed
114when the virtual environment is created.
Vinay Sajipc4618e32012-07-10 08:21:07 +0100115
R David Murray575fb312013-12-25 23:21:03 -0500116.. versionadded:: 3.4
117 ``fish`` and ``csh`` activation scripts.