blob: 4c7795ad801964fe945f58771efb36763d67ec3c [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``
TROUVERIE Joachime8eb9722018-02-18 17:52:36 +010012subdirectory (on Windows, this is ``Lib\site-packages``). If an existing
13directory is specified, it will be re-used.
Vinay Sajipc4618e32012-07-10 08:21:07 +010014
Brett Cannon15552c32016-07-08 10:46:21 -070015.. deprecated:: 3.6
16 ``pyvenv`` was the recommended tool for creating virtual environments for
17 Python 3.3 and 3.4, and is `deprecated in Python 3.6
18 <https://docs.python.org/dev/whatsnew/3.6.html#deprecated-features>`_.
19
20.. versionchanged:: 3.5
21 The use of ``venv`` is now recommended for creating virtual environments.
22
Larry Hastings3732ed22014-03-15 21:13:56 -070023.. seealso::
24
25 `Python Packaging User Guide: Creating and using virtual environments
Brett Cannon15552c32016-07-08 10:46:21 -070026 <https://packaging.python.org/installing/#creating-virtual-environments>`__
Larry Hastings3732ed22014-03-15 21:13:56 -070027
Vinay Sajipc4618e32012-07-10 08:21:07 +010028.. highlight:: none
29
Brett Cannon15552c32016-07-08 10:46:21 -070030On Windows, invoke the ``venv`` command as follows::
Vinay Sajipc4618e32012-07-10 08:21:07 +010031
Brett Cannon15552c32016-07-08 10:46:21 -070032 c:\>c:\Python35\python -m venv c:\path\to\myenv
Vinay Sajipc4618e32012-07-10 08:21:07 +010033
Brett Cannon15552c32016-07-08 10:46:21 -070034Alternatively, if you configured the ``PATH`` and ``PATHEXT`` variables for
35your :ref:`Python installation <using-on-windows>`::
Vinay Sajipc4618e32012-07-10 08:21:07 +010036
cocoatomod609b0c2017-10-27 13:42:11 +090037 c:\>python -m venv c:\path\to\myenv
Vinay Sajipc4618e32012-07-10 08:21:07 +010038
39The command, if run with ``-h``, will show the available options::
40
Berker Peksaga9744ae2016-01-30 12:17:10 +020041 usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
42 [--upgrade] [--without-pip]
43 ENV_DIR [ENV_DIR ...]
Vinay Sajipc4618e32012-07-10 08:21:07 +010044
45 Creates virtual Python environments in one or more target directories.
46
47 positional arguments:
Brett Cannon15552c32016-07-08 10:46:21 -070048 ENV_DIR A directory to create the environment in.
Vinay Sajipc4618e32012-07-10 08:21:07 +010049
50 optional arguments:
Brett Cannon15552c32016-07-08 10:46:21 -070051 -h, --help show this help message and exit
52 --system-site-packages
53 Give the virtual environment access to the system
54 site-packages dir.
55 --symlinks Try to use symlinks rather than copies, when symlinks
56 are not the default for the platform.
57 --copies Try to use copies rather than symlinks, even when
58 symlinks are the default for the platform.
59 --clear Delete the contents of the environment directory if it
60 already exists, before environment creation.
61 --upgrade Upgrade the environment directory to use this version
62 of Python, assuming Python has been upgraded in-place.
63 --without-pip Skips installing or upgrading pip in the virtual
64 environment (pip is bootstrapped by default)
Nick Coghlan8fbdb092013-11-23 00:30:34 +100065
Brett Cannon15552c32016-07-08 10:46:21 -070066 Once an environment has been created, you may wish to activate it, e.g. by
67 sourcing an activate script in its bin directory.
Vinay Sajipc7e34fb2015-02-07 10:52:02 +000068
Nick Coghlan8fbdb092013-11-23 00:30:34 +100069.. versionchanged:: 3.4
Larry Hastings3732ed22014-03-15 21:13:56 -070070 Installs pip by default, added the ``--without-pip`` and ``--copies``
71 options
Vinay Sajipc4618e32012-07-10 08:21:07 +010072
Vinay Sajip71e72962015-01-23 19:35:12 +000073.. versionchanged:: 3.4
74 In earlier versions, if the target directory already existed, an error was
TROUVERIE Joachime8eb9722018-02-18 17:52:36 +010075 raised, unless the ``--clear`` or ``--upgrade`` option was provided.
Vinay Sajipc4618e32012-07-10 08:21:07 +010076
77The created ``pyvenv.cfg`` file also includes the
78``include-system-site-packages`` key, set to ``true`` if ``venv`` is
79run with the ``--system-site-packages`` option, ``false`` otherwise.
80
Nick Coghlan8fbdb092013-11-23 00:30:34 +100081Unless the ``--without-pip`` option is given, :mod:`ensurepip` will be
82invoked to bootstrap ``pip`` into the virtual environment.
83
Brett Cannon15552c32016-07-08 10:46:21 -070084Multiple paths can be given to ``venv``, in which case an identical virtual
85environment will be created, according to the given options, at each provided
86path.
Vinay Sajipc4618e32012-07-10 08:21:07 +010087
Brett Cannon15552c32016-07-08 10:46:21 -070088Once a virtual environment has been created, it can be "activated" using a
89script in the virtual environment's binary directory. The invocation of the
90script is platform-specific:
Vinay Sajipc4618e32012-07-10 08:21:07 +010091
Andrew Svetlov65e9c572012-10-04 21:48:58 +030092+-------------+-----------------+-----------------------------------------+
93| Platform | Shell | Command to activate virtual environment |
94+=============+=================+=========================================+
95| Posix | bash/zsh | $ source <venv>/bin/activate |
96+-------------+-----------------+-----------------------------------------+
97| | fish | $ . <venv>/bin/activate.fish |
98+-------------+-----------------+-----------------------------------------+
99| | csh/tcsh | $ source <venv>/bin/activate.csh |
100+-------------+-----------------+-----------------------------------------+
Berker Peksag6803f352016-06-27 13:10:47 +0300101| Windows | cmd.exe | C:\\> <venv>\\Scripts\\activate.bat |
Andrew Svetlov65e9c572012-10-04 21:48:58 +0300102+-------------+-----------------+-----------------------------------------+
Berker Peksag6803f352016-06-27 13:10:47 +0300103| | PowerShell | PS C:\\> <venv>\\Scripts\\Activate.ps1 |
Andrew Svetlov65e9c572012-10-04 21:48:58 +0300104+-------------+-----------------+-----------------------------------------+
Vinay Sajipc4618e32012-07-10 08:21:07 +0100105
106You don't specifically *need* to activate an environment; activation just
Brett Cannon15552c32016-07-08 10:46:21 -0700107prepends the virtual environment's binary directory to your path, so that
108"python" invokes the virtual environment's Python interpreter and you can run
109installed scripts without having to use their full path. However, all scripts
110installed in a virtual environment should be runnable without activating it,
111and run with the virtual environment's Python automatically.
Vinay Sajipc4618e32012-07-10 08:21:07 +0100112
Brett Cannon15552c32016-07-08 10:46:21 -0700113You can deactivate a virtual environment by typing "deactivate" in your shell.
114The exact mechanism is platform-specific: for example, the Bash activation
115script defines a "deactivate" function, whereas on Windows there are separate
116scripts called ``deactivate.bat`` and ``Deactivate.ps1`` which are installed
117when the virtual environment is created.
Vinay Sajipc4618e32012-07-10 08:21:07 +0100118
R David Murray575fb312013-12-25 23:21:03 -0500119.. versionadded:: 3.4
120 ``fish`` and ``csh`` activation scripts.