• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1Creation of :ref:`virtual environments <venv-def>` is done by executing the
2command ``venv``::
3
4    python3 -m venv /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 from which the command
9was run (a common name for the target directory is ``.venv``).  It also creates
10a ``bin`` (or ``Scripts`` on Windows) subdirectory containing a copy/symlink
11of the Python binary/binaries (as appropriate for the platform or arguments
12used at environment creation time). It also creates an (initially empty)
13``lib/pythonX.Y/site-packages`` subdirectory (on Windows, this is
14``Lib\site-packages``). If an existing directory is specified, it will be
15re-used.
16
17.. deprecated:: 3.6
18   ``pyvenv`` was the recommended tool for creating virtual environments for
19   Python 3.3 and 3.4, and is `deprecated in Python 3.6
20   <https://docs.python.org/dev/whatsnew/3.6.html#deprecated-features>`_.
21
22.. versionchanged:: 3.5
23   The use of ``venv`` is now recommended for creating virtual environments.
24
25.. highlight:: none
26
27On Windows, invoke the ``venv`` command as follows::
28
29    c:\>c:\Python35\python -m venv c:\path\to\myenv
30
31Alternatively, if you configured the ``PATH`` and ``PATHEXT`` variables for
32your :ref:`Python installation <using-on-windows>`::
33
34    c:\>python -m venv c:\path\to\myenv
35
36The command, if run with ``-h``, will show the available options::
37
38    usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
39                [--upgrade] [--without-pip] [--prompt PROMPT]
40                ENV_DIR [ENV_DIR ...]
41
42    Creates virtual Python environments in one or more target directories.
43
44    positional arguments:
45      ENV_DIR               A directory to create the environment in.
46
47    optional arguments:
48      -h, --help            show this help message and exit
49      --system-site-packages
50                            Give the virtual environment access to the system
51                            site-packages dir.
52      --symlinks            Try to use symlinks rather than copies, when symlinks
53                            are not the default for the platform.
54      --copies              Try to use copies rather than symlinks, even when
55                            symlinks are the default for the platform.
56      --clear               Delete the contents of the environment directory if it
57                            already exists, before environment creation.
58      --upgrade             Upgrade the environment directory to use this version
59                            of Python, assuming Python has been upgraded in-place.
60      --without-pip         Skips installing or upgrading pip in the virtual
61                            environment (pip is bootstrapped by default)
62      --prompt PROMPT       Provides an alternative prompt prefix for this
63                            environment.
64
65    Once an environment has been created, you may wish to activate it, e.g. by
66    sourcing an activate script in its bin directory.
67
68.. versionchanged:: 3.4
69   Installs pip by default, added the ``--without-pip``  and ``--copies``
70   options
71
72.. 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.
75
76.. note::
77   While symlinks are supported on Windows, they are not recommended. Of
78   particular note is that double-clicking ``python.exe`` in File Explorer
79   will resolve the symlink eagerly and ignore the virtual environment.
80
81.. note::
82   On Microsoft Windows, it may be required to enable the ``Activate.ps1``
83   script by setting the execution policy for the user. You can do this by
84   issuing the following PowerShell command:
85
86   PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
87
88   See `About Execution Policies
89   <https://go.microsoft.com/fwlink/?LinkID=135170>`_
90   for more information.
91
92The created ``pyvenv.cfg`` file also includes the
93``include-system-site-packages`` key, set to ``true`` if ``venv`` is
94run with the ``--system-site-packages`` option, ``false`` otherwise.
95
96Unless the ``--without-pip`` option is given, :mod:`ensurepip` will be
97invoked to bootstrap ``pip`` into the virtual environment.
98
99Multiple paths can be given to ``venv``, in which case an identical virtual
100environment will be created, according to the given options, at each provided
101path.
102
103Once a virtual environment has been created, it can be "activated" using a
104script in the virtual environment's binary directory. The invocation of the
105script is platform-specific (`<venv>` must be replaced by the path of the
106directory containing the virtual environment):
107
108+-------------+-----------------+-----------------------------------------+
109| Platform    | Shell           | Command to activate virtual environment |
110+=============+=================+=========================================+
111| POSIX       | bash/zsh        | $ source <venv>/bin/activate            |
112+-------------+-----------------+-----------------------------------------+
113|             | fish            | $ . <venv>/bin/activate.fish            |
114+-------------+-----------------+-----------------------------------------+
115|             | csh/tcsh        | $ source <venv>/bin/activate.csh        |
116+-------------+-----------------+-----------------------------------------+
117|             | PowerShell Core | $ <venv>/bin/Activate.ps1               |
118+-------------+-----------------+-----------------------------------------+
119| Windows     | cmd.exe         | C:\\> <venv>\\Scripts\\activate.bat     |
120+-------------+-----------------+-----------------------------------------+
121|             | PowerShell      | PS C:\\> <venv>\\Scripts\\Activate.ps1  |
122+-------------+-----------------+-----------------------------------------+
123
124You don't specifically *need* to activate an environment; activation just
125prepends the virtual environment's binary directory to your path, so that
126"python" invokes the virtual environment's Python interpreter and you can run
127installed scripts without having to use their full path. However, all scripts
128installed in a virtual environment should be runnable without activating it,
129and run with the virtual environment's Python automatically.
130
131You can deactivate a virtual environment by typing "deactivate" in your shell.
132The exact mechanism is platform-specific and is an internal implementation
133detail (typically a script or shell function will be used).
134
135.. versionadded:: 3.4
136   ``fish`` and ``csh`` activation scripts.
137
138.. versionadded:: 3.8
139   PowerShell activation scripts installed under POSIX for PowerShell Core
140   support.
141