Skip to content

Commit 3bf7c90

Browse files
author
Kenneth Reitz
committed
Merge pull request realpython#68 from john2x/virtualenvs
Flesh out virtualenvs section.
2 parents 382d849 + 7e3b364 commit 3bf7c90

File tree

1 file changed

+116
-3
lines changed

1 file changed

+116
-3
lines changed

docs/dev/virtualenvs.rst

Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,128 @@
11
Virtual Environments
22
====================
33

4-
.. todo:: Explain "Virtual Environments"
4+
A Virtual Environment, put simply, is an isolated working copy of Python which
5+
allows you to work on a specific project without worry of affecting other
6+
projects.
7+
8+
For example, you can work on a project which requires Django 1.3 while also
9+
maintaining a project which requires Django 1.0.
510

611
virtualenv
712
----------
813

9-
.. todo:: Write about virtualenv
14+
`virtualenv <http://pypi.python.org/pypi/virtualenv>`_ is a tool to create
15+
isolated Python environments.
16+
17+
Install it via pip:
18+
19+
.. code-block:: console
20+
21+
$ pip install virtualenv
22+
23+
Basic Usage
24+
~~~~~~~~~~~
25+
26+
1. Create a virtual environment:
27+
28+
.. code-block:: console
29+
30+
$ virtualenv venv
31+
32+
This creates a copy of Python in whichever directory you ran the command in,
33+
placing it in a folder named ``venv``.
34+
35+
2. To begin using the virtual environment, it needs to be activated:
36+
37+
.. code-block:: console
38+
39+
$ source venv/bin/activate
40+
41+
You can then begin installing any new modules without affecting the system
42+
default Python or other virtual environments.
43+
44+
3. If you are done working in the virtual environment for the moment, you can
45+
deactivate it:
46+
47+
.. code-block:: console
48+
49+
$ deactivate
50+
51+
This puts you back to the system's default Python interpreter with all its
52+
installed libraries.
53+
54+
To delete a virtual environment, just delete its folder.
55+
56+
After a while, though, you might end up with a lot of virtual environments
57+
littered across your system, and its possible you'll forget their names or
58+
where they were placed.
1059

1160
virtualenvwrapper
1261
-----------------
1362

14-
.. todo:: Write about virtualenvwrapper
63+
`virtualenvwrapper <http://www.doughellmann.com/projects/virtualenvwrapper/>`_
64+
provides a set of commands which makes working with virtual environments much
65+
more pleasant. It also places all your virtual environments in one place.
66+
67+
To install (make sure **virtualenv** is already installed):
68+
69+
.. code-block:: console
70+
71+
$ pip install virtualenvwrapper
72+
$ export WORKON_HOME=~/Envs
73+
$ source /usr/local/bin/virtualenvwrapper.sh
74+
75+
(`Full virtualenvwrapper install instructions <http://www.doughellmann.com/docs/virtualenvwrapper/#introduction>`_.)
76+
77+
Basic Usage
78+
~~~~~~~~~~~
79+
80+
1. Create a virtual environment:
81+
82+
.. code-block:: console
83+
84+
$ mkvirtualenv venv
85+
86+
This creates the ``venv`` folder inside ``~/Envs``.
87+
88+
2. Work on a virtual environment:
89+
90+
.. code-block:: console
91+
92+
$ workon venv
93+
94+
**virtualenvwrapper** provides tab-completion on environment names. It really
95+
helps when you have a lot of environments and have trouble remembering their
96+
names.
97+
``workon`` also deactivates whatever environment you are currently in, so you
98+
can quickly switch between environments.
99+
100+
3. Deactivating is still the same:
101+
102+
.. code-block:: console
103+
104+
$ deactivate
105+
106+
4. To delete:
107+
108+
.. code-block:: console
109+
110+
$ rmvirtualenv venv
111+
112+
Other useful commands
113+
~~~~~~~~~~~~~~~~~~~~~
114+
115+
``lsvirtualenv``
116+
List all of the environments.
117+
118+
``cdvirtualenv``
119+
Navigate into the directory of the currently activated virtual environment,
120+
so you can browse its ``site-packages``, for example.
121+
122+
``cdsitepackages``
123+
Like the above, but directly into ``site-packages`` directory.
124+
125+
``lssitepackages``
126+
Shows contents of ``site-packages`` directory.
15127

128+
`Full list of virtualenvwrapper commands <http://www.doughellmann.com/docs/virtualenvwrapper/command_ref.html#managing-environments>`_.

0 commit comments

Comments
 (0)