1- Installing packages using pip and virtualenv
2- ============================================
1+ Installing packages using pip and virtual environments
2+ ======================================================
33
44This guide discusses how to install packages using :ref: `pip ` and
5- :ref: `virtualenv `. These are the lowest-level tools for managing Python
5+ a virtual environment manager: either :ref: `venv ` for Python 3 or :ref: `virtualenv `
6+ for Python 2. These are the lowest-level tools for managing Python
67packages and are recommended if higher-level tools do not suit your needs.
78
89.. note :: This doc uses the term **package** to refer to a
@@ -63,6 +64,12 @@ Afterwards, you should have the newest pip installed in your user site:
6364Installing virtualenv
6465---------------------
6566
67+ .. Note :: If you are using Python 3.3 or newer, the :mod:`venv` module is
68+ the preferred way to create and manage virtual environments.
69+ venv is included in the Python standard library and requires no additional installation.
70+ If you are using venv, you may skip this section.
71+
72+
6673:ref: `virtualenv ` is used to manage Python packages for different projects.
6774Using virtualenv allows you to avoid installing Python packages globally
6875which could break system tools or other projects. You can install virtualenv
@@ -81,51 +88,50 @@ On Windows:
8188 py -m pip install --user virtualenv
8289
8390
84- .. Note :: If you are using Python 3.3 or newer the :mod:`venv` module is
85- included in the Python standard library. This can also create and manage
86- virtual environments, however, it only supports Python 3.
87-
8891
89- Creating a virtualenv
90- ---------------------
92+ Creating a virtual environment
93+ ------------------------------
9194
92- :ref: `virtualenv ` allows you to manage separate package installations for
93- different projects. It essentially allows you to create a "virtual" isolated
95+ :ref: `venv ` (for Python 3) and :ref: `virtualenv ` (for Python 2) allow
96+ you to manage separate package installations for
97+ different projects. They essentially allow you to create a "virtual" isolated
9498Python installation and install packages into that virtual installation. When
9599you switch projects, you can simply create a new virtual environment and not
96100have to worry about breaking the packages installed in the other environments.
97- It is always recommended to use a virtualenv while developing Python
101+ It is always recommended to use a virtual environment while developing Python
98102applications.
99103
100104To create a virtual environment, go to your project's directory and run
101- virtualenv.
105+ venv. If you are using Python 2, replace ``venv `` with ``virtualenv ``
106+ in the below commands.
102107
103108On macOS and Linux:
104109
105110.. code-block :: bash
106111
107- python3 -m virtualenv env
112+ python3 -m venv env
108113
109114 On Windows:
110115
111116.. code-block :: bash
112117
113- py -m virtualenv env
118+ py -m venv env
114119
115- The second argument is the location to create the virtualenv . Generally, you
120+ The second argument is the location to create the virtual environment . Generally, you
116121can just create this in your project and call it ``env ``.
117122
118- virtualenv will create a virtual Python installation in the ``env `` folder.
123+ venv will create a virtual Python installation in the ``env `` folder.
119124
120- .. Note :: You should exclude your virtualenv directory from your version
125+ .. Note :: You should exclude your virtual environment directory from your version
121126 control system using ``.gitignore `` or similar.
122127
123128
124- Activating a virtualenv
125- -----------------------
129+ Activating a virtual environment
130+ --------------------------------
126131
127- Before you can start installing or using packages in your virtualenv you'll
128- need to *activate * it. Activating a virtualenv will put the virtualenv-specific
132+ Before you can start installing or using packages in your virtual environment you'll
133+ need to *activate * it. Activating a virtual environment will put the
134+ virtual environment-specific
129135``python `` and ``pip `` executables into your shell's ``PATH ``.
130136
131137On macOS and Linux:
@@ -138,7 +144,7 @@ On Windows::
138144
139145 .\env\Scripts\activate
140146
141- You can confirm you're in the virtualenv by checking the location of your
147+ You can confirm you're in the virtual environment by checking the location of your
142148Python interpreter, it should point to the ``env `` directory.
143149
144150On macOS and Linux:
@@ -156,28 +162,28 @@ On Windows:
156162 .../env/bin/python.exe
157163
158164
159- As long as your virtualenv is activated pip will install packages into that
165+ As long as your virtual environment is activated pip will install packages into that
160166specific environment and you'll be able to import and use packages in your
161167Python application.
162168
163169
164- Leaving the virtualenv
165- ----------------------
170+ Leaving the virtual environment
171+ -------------------------------
166172
167- If you want to switch projects or otherwise leave your virtualenv , simply run:
173+ If you want to switch projects or otherwise leave your virtual environment , simply run:
168174
169175.. code-block :: bash
170176
171177 deactivate
172178
173- If you want to re-enter the virtualenv just follow the same instructions above
174- about activating a virtualenv . There's no need to re-create the virtualenv .
179+ If you want to re-enter the virtual environment just follow the same instructions above
180+ about activating a virtual environment . There's no need to re-create the virtual environment .
175181
176182
177183Installing packages
178184-------------------
179185
180- Now that you're in your virtualenv you can install packages. Let's install the
186+ Now that you're in your virtual environment you can install packages. Let's install the
181187excellent `Requests `_ library from the :term: `Python Package Index (PyPI) `:
182188
183189.. code-block :: bash
0 commit comments