Skip to content

Commit 10afe05

Browse files
committedJan 19, 2020
simplify virtual environment instructions
1 parent cfec971 commit 10afe05

File tree

3 files changed

+37
-138
lines changed

3 files changed

+37
-138
lines changed
 

‎docs/contents.rst.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ New to Python? Let's properly setup up your Python environment:
2222
starting/install/linux
2323
2424
25-
- Using Virtualenvs with Pipenv:
25+
- Package Installation and Project Isolation:
2626
2727
.. toctree::
2828
:maxdepth: 2

‎docs/dev/pip-virtualenv.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.. _pip-virtualenv:
22

3-
Further Configuration of pip and Virtualenv
3+
Further Configuration of pip and virtual environments
44
===========================================
55

66
.. image:: /_static/photos/34018732105_f0e6758859_k_d.jpg

‎docs/dev/virtualenvs.rst

+35-136
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. _virtualenvironments-ref:
22

3-
Pipenv & Virtual Environments
4-
=============================
3+
Package Installation and Project Isolation
4+
==========================================
55

66
.. image:: /_static/photos/35294660055_42c02b2316_k_d.jpg
77

@@ -65,6 +65,9 @@ using your OS package manager, you may have to `install pip <https://pip.pypa.io
6565
.. _Installing Python: https://docs.python-guide.org/starting/installation/
6666

6767

68+
Pipenv
69+
======
70+
6871
Installing Pipenv
6972
-----------------
7073

@@ -197,27 +200,17 @@ Congratulations, you now know how to install and use Python packages! ✨ 🍰
197200

198201

199202

200-
Lower level: virtualenv
203+
Project Isolation with Virtual Environments
201204
=======================
202205

203-
`virtualenv <http://pypi.org/project/virtualenv>`_ is a tool to create
204-
isolated Python environments. virtualenv creates a folder which contains all the
205-
necessary executables to use the packages that a Python project would need.
206+
If you choose not to use Pipenv or it does not fit your needs, you can
207+
use the `venv <https://docs.python.org/3/library/venv.html>`_ tool directly to create
208+
isolated Python environments. The ``venv`` module is part of Python's standard library,
209+
and was introduced in Python 3.3. It creates a folder which contains all the necessary
210+
executables to use the packages that a Python project would need.
206211

207212
It can be used standalone, in place of Pipenv.
208213

209-
Install virtualenv via pip:
210-
211-
.. code-block:: console
212-
213-
$ pip install virtualenv
214-
215-
Test your installation:
216-
217-
.. code-block:: console
218-
219-
$ virtualenv --version
220-
221214
Basic Usage
222215
-----------
223216

@@ -226,45 +219,47 @@ Basic Usage
226219
.. code-block:: console
227220
228221
$ cd project_folder
229-
$ virtualenv venv
222+
$ python -m venv venv
230223
231-
``virtualenv venv`` will create a folder in the current directory which will
232-
contain the Python executable files, and a copy of the ``pip`` library which you
224+
``python -m venv venv`` will create a folder in the current directory which will
225+
contain the Python executable files, and a copy of the ``pip`` application which you
233226
can use to install other packages. The name of the virtual environment (in this
234-
case, it was ``venv``) can be anything; omitting the name will place the files
235-
in the current directory instead.
227+
case, it was ``venv``) can be anything.
236228

237229
.. note::
238230
'venv' is the general convention used globally. As it is readily available in ignore files (eg: .gitignore')
239231

240232
This creates a copy of Python in whichever directory you ran the command in,
241233
placing it in a folder named :file:`venv`.
242234

243-
You can also use the Python interpreter of your choice (like
244-
``python2.7``).
235+
You can also use the Python interpreter of your choice (like ``python3.8``).
245236

246237
.. code-block:: console
247238
248-
$ virtualenv -p /usr/bin/python2.7 venv
239+
$ python3.8 -m venv venv
249240
250-
or change the interpreter globally with an env variable in ``~/.bashrc``:
251241
252-
.. code-block:: console
242+
2. To begin using the virtual environment, you can either invoke the virtual environment's executables
243+
directly, or activate it.
253244

254-
$ export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7
245+
To use the virtual environment's Python executable directly, run
246+
``venv/bin/python``; to use its pip executable, ``venv/bin/pip``.
255247

256-
2. To begin using the virtual environment, it needs to be activated:
248+
Alternatively, you can "activate"
249+
the environment so you can just type ``python`` or ``pip`` and it will automatically use the
250+
executables in the virtual environment (in this case, at ``venv/bin``).
257251

258252
.. code-block:: console
259253
260254
$ source venv/bin/activate
261255
262-
The name of the current virtual environment will now appear on the left of
256+
Now, the name of the current virtual environment will appear on the left of
263257
the prompt (e.g. ``(venv)Your-Computer:project_folder UserName$``) to let you know
264258
that it's active. From now on, any package that you install using pip will be
265259
placed in the ``venv`` folder, isolated from the global Python installation.
266260

267-
For Windows, the same command mentioned in step 1 can be used to create a virtual environment. However, activating the environment requires a slightly different command.
261+
For Windows, the same command mentioned in step 1 can be used to create a virtual environment.
262+
However, activating the environment requires a slightly different command.
268263

269264
Assuming that you are in your project directory:
270265

@@ -286,25 +281,26 @@ Install packages using the ``pip`` command:
286281
$ deactivate
287282
288283
This puts you back to the system's default Python interpreter with all its
289-
installed libraries.
284+
installed libraries. This is not necessary if you invoked the executables directly.
290285

291286
To delete a virtual environment, just delete its folder. (In this case,
292287
it would be ``rm -rf venv``.)
293288

294289
After a while, though, you might end up with a lot of virtual environments
295-
littered across your system, and it's possible you'll forget their names or
296-
where they were placed.
290+
littered across your system. It's possible you'll forget their names or
291+
where they were placed, so try to follow a convention across your projects.
297292

298293
.. note::
299-
Python has included venv module from version 3.3. For more details: `venv <https://docs.python.org/3/library/venv.html>`_.
294+
The ``venv`` module is part of Python's standard library in Python3.3+.
295+
Older versions of Python can use the
296+
3rd party package `virtualenv <https://pypi.org/project/virtualenv/>`_.
300297

301298
Other Notes
302299
-----------
303300

304-
Running ``virtualenv`` with the option ``--no-site-packages`` will not
301+
Running ``python -m venv`` with the option ``--no-site-packages`` will not
305302
include the packages that are installed globally. This can be useful
306303
for keeping the package list clean in case it needs to be accessed later.
307-
[This is the default behavior for ``virtualenv`` 1.7 and later.]
308304

309305
In order to keep your environment consistent, it's a good idea to "freeze"
310306
the current state of the environment packages. To do this, run:
@@ -330,103 +326,6 @@ and across developers.
330326
Lastly, remember to exclude the virtual environment folder from source
331327
control by adding it to the ignore list (see :ref:`Version Control Ignores<version_control_ignores>`).
332328

333-
.. _virtualenvwrapper-ref:
334-
335-
virtualenvwrapper
336-
-----------------
337-
338-
`virtualenvwrapper <https://virtualenvwrapper.readthedocs.io/en/latest/index.html>`_
339-
provides a set of commands which makes working with virtual environments much
340-
more pleasant. It also places all your virtual environments in one place.
341-
342-
To install (make sure **virtualenv** is already installed):
343-
344-
.. code-block:: console
345-
346-
$ pip install virtualenvwrapper
347-
$ export WORKON_HOME=~/Envs
348-
$ source /usr/local/bin/virtualenvwrapper.sh
349-
350-
(`Full virtualenvwrapper install instructions <https://virtualenvwrapper.readthedocs.io/en/latest/install.html>`_.)
351-
352-
For Windows, you can use the `virtualenvwrapper-win <https://github.com/davidmarble/virtualenvwrapper-win/>`_.
353-
354-
To install (make sure **virtualenv** is already installed):
355-
356-
.. code-block:: console
357-
358-
$ pip install virtualenvwrapper-win
359-
360-
In Windows, the default path for WORKON_HOME is %USERPROFILE%\\Envs
361-
362-
Basic Usage
363-
~~~~~~~~~~~
364-
365-
1. Create a virtual environment:
366-
367-
.. code-block:: console
368-
369-
$ mkvirtualenv project_folder
370-
371-
This creates the :file:`project_folder` folder inside :file:`~/Envs`.
372-
373-
2. Work on a virtual environment:
374-
375-
.. code-block:: console
376-
377-
$ workon project_folder
378-
379-
Alternatively, you can make a project, which creates the virtual environment,
380-
and also a project directory inside ``$WORKON_HOME``, which is ``cd``-ed into
381-
when you ``workon project_folder``.
382-
383-
.. code-block:: console
384-
385-
$ mkproject project_folder
386-
387-
**virtualenvwrapper** provides tab-completion on environment names. It really
388-
helps when you have a lot of environments and have trouble remembering their
389-
names.
390-
391-
``workon`` also deactivates whatever environment you are currently in, so you
392-
can quickly switch between environments.
393-
394-
3. Deactivating is still the same:
395-
396-
.. code-block:: console
397-
398-
$ deactivate
399-
400-
4. To delete:
401-
402-
.. code-block:: console
403-
404-
$ rmvirtualenv venv
405-
406-
Other useful commands
407-
~~~~~~~~~~~~~~~~~~~~~
408-
409-
``lsvirtualenv``
410-
List all of the environments.
411-
412-
``cdvirtualenv``
413-
Navigate into the directory of the currently activated virtual environment,
414-
so you can browse its :file:`site-packages`, for example.
415-
416-
``cdsitepackages``
417-
Like the above, but directly into :file:`site-packages` directory.
418-
419-
``lssitepackages``
420-
Shows contents of :file:`site-packages` directory.
421-
422-
`Full list of virtualenvwrapper commands <https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html>`_.
423-
424-
virtualenv-burrito
425-
------------------
426-
427-
With `virtualenv-burrito <https://github.com/brainsik/virtualenv-burrito>`_, you
428-
can have a working virtualenv + virtualenvwrapper environment in a single command.
429-
430329
direnv
431330
-------
432331
When you ``cd`` into a directory containing a :file:`.env`, `direnv <https://direnv.net>`_
@@ -438,4 +337,4 @@ Install it on Mac OS X using ``brew``:
438337
439338
$ brew install direnv
440339
441-
On Linux follow the instructions at `direnv.net <https://direnv.net>`
340+
On Linux follow the instructions at `direnv.net <https://direnv.net>`_

0 commit comments

Comments
 (0)