11Virtual 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
89For example, you can work on a project which requires Django 1.3 while also
910maintaining 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
1719Install 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+
3239This creates a copy of Python in whichever directory you ran the command in,
3340placing 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
52613. 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
6069installed 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
6474After a while, though, you might end up with a lot of virtual environments
6575littered across your system, and its possible you'll forget their names or
6676where 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+
68109virtualenvwrapper
69110-----------------
70111
0 commit comments