Skip to content

Commit 8e6bec7

Browse files
committed
Consolidating info from dev/env.rst on virtualenv
1 parent 2f56b67 commit 8e6bec7

File tree

1 file changed

+50
-9
lines changed

1 file changed

+50
-9
lines changed

docs/dev/virtualenvs.rst

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
Virtual Environments
22
====================
33

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.
4+
A Virtual Environment is a tool to keep the dependencies required by different projects
5+
in separate places, by creating virtual Python environments for them. It solves the
6+
"Project X depends on version 1.x but, Project Y needs 4.x" dilemma, and keeps
7+
your global site-packages directory clean and manageable.
78

89
For example, you can work on a project which requires Django 1.3 while also
910
maintaining a project which requires Django 1.0.
@@ -12,7 +13,8 @@ virtualenv
1213
----------
1314

1415
`virtualenv <http://pypi.python.org/pypi/virtualenv>`_ is a tool to create
15-
isolated Python environments.
16+
isolated Python environments. virtualenv creates a folder which contains all the
17+
necessary executables to use the packages that a Python project would need.
1618

1719
Install it via pip:
1820

@@ -23,12 +25,17 @@ Install it via pip:
2325
Basic Usage
2426
~~~~~~~~~~~
2527

26-
1. Create a virtual environment:
28+
1. Create a virtual environment for a project:
2729

2830
.. code-block:: console
29-
31+
$ cd my_project_folder
3032
$ virtualenv venv
3133
34+
``virtualenv venv`` will create a folder in the current directory which will contain
35+
the Python executable files, and a copy of the ``pip`` library which you can use to
36+
install other packages. The name of the virtual environment (in this case, it was ``venv``)
37+
can be anything; omitting the name will place the files in the current directory instead.
38+
3239
This creates a copy of Python in whichever directory you ran the command in,
3340
placing it in a folder named :file:`venv`.
3441

@@ -46,8 +53,10 @@ This will use the Python interpreter in :file:`/usr/bin/python2.7`
4653
4754
$ source venv/bin/activate
4855
49-
You can then begin installing any new modules without affecting the system
50-
default Python or other virtual environments.
56+
The name of the current virtual environment will now appear on the left of
57+
the prompt (e.g. ``(venv)Your-Computer:your_project UserName$)`` to let you know
58+
that it's active. From now on, any package that you install using pip will be
59+
placed in the ``venv`` folder, isolated from the global Python installation.
5160

5261
3. If you are done working in the virtual environment for the moment, you can
5362
deactivate it:
@@ -59,12 +68,44 @@ default Python or other virtual environments.
5968
This puts you back to the system's default Python interpreter with all its
6069
installed libraries.
6170

62-
To delete a virtual environment, just delete its folder.
71+
To delete a virtual environment, just delete its folder. (In this case,
72+
it would be ``rm -rf venv``.)
6373

6474
After a while, though, you might end up with a lot of virtual environments
6575
littered across your system, and its possible you'll forget their names or
6676
where they were placed.
6777

78+
Other Notes
79+
^^^^^^^^^^^
80+
81+
Running ``virtualenv`` with the option :option:`--no-site-packages` will not
82+
include the packages that are installed globally. This can be useful
83+
for keeping the package list clean in case it needs to be accessed later.
84+
[This is the default behavior for ``virtualenv`` 1.7 and later.]
85+
86+
In order to keep your environment consistent, it's a good idea to "freeze"
87+
the current state of the environment packages. To do this, run
88+
89+
.. code-block:: console
90+
91+
$ pip freeze > requirements.txt
92+
93+
This will create a :file:`requirements.txt` file, which contains a simple
94+
list of all the packages in the current environment, and their respective
95+
versions. Later it will be easier for a different developer (or you, if you
96+
need to re-create the environment) to install the same packages using the
97+
same versions:
98+
99+
.. code-block:: console
100+
101+
$ pip install -r requirements.txt
102+
103+
This can help ensure consistency across installations, across deployments,
104+
and across developers.
105+
106+
Lastly, remember to exclude the virtual environment folder from source
107+
control by adding it to the ignore list.
108+
68109
virtualenvwrapper
69110
-----------------
70111

0 commit comments

Comments
 (0)