Skip to content

Commit fedc03c

Browse files
author
Kenneth Reitz
committed
Merge pull request realpython#45 from gggritso/virtualenv-details
Detailed virtualenv example
2 parents e701557 + 4043a5e commit fedc03c

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

docs/starting/dev-env.rst

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,91 @@ virtualenv
9797
Virtualenv is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them.
9898
It solves the "Project X depends on version 1.x but, Project Y needs 4.x" dilemma and keeps your global site-packages directory clean and manageable.
9999

100+
`virtualenv <http://www.virtualenv.org/en/latest/index.html>`_ creates
101+
a folder which contains all the necessary executables to contain the
102+
packages that a Python project would need. An example workflow is given.
103+
104+
Install virtualenv
105+
106+
::
107+
108+
$ pip install virtualenv
109+
110+
or, depending on what's available
111+
112+
::
113+
114+
$ easy_install virtualenv
115+
116+
Create a virtual environment for a project
117+
118+
::
119+
120+
$ cd my_project
121+
$ virtualenv venv
122+
123+
``virtualenv venv`` will create a folder in the currect directory
124+
which will contain the Python executable files, and a copy of the ``pip``
125+
library which you can use to install other packages. The name of the
126+
virtual environment (in this case, it was ``venv``) can be anything;
127+
omitting the name will place the files in the current directory instead.
128+
129+
In order the start using the virtual environment, run
130+
131+
::
132+
133+
$ source venv/bin/activate
134+
135+
or
136+
137+
::
138+
139+
$ . venv/bin/activate
140+
141+
The name of the current virtual environment will now appear on the left
142+
of the prompt (e.g. ``(venv)Your-Computer:your_project UserName$``) to
143+
let you know that it's active. From now on, any package that you install
144+
using ``pip`` will be placed in the venv folder, isolated from the global
145+
Python installation. Install packages as usual.
146+
147+
::
148+
149+
$ pip install requests
150+
151+
To stop using an environment simply type ``deactivate``. To remove the
152+
environment, just remove the directory it was installed into. (In this
153+
case, it would be ``rm -rf venv``).
154+
155+
Other Notes
156+
~~~~~~~~~~~
157+
158+
Running ``virtualenv`` with the option ``--no-site-packages`` will not
159+
include the packages that are installed globally. This can be useful
160+
for keeping the package list clean in case it needs to be accessed later.
161+
162+
In order to keep your environment consistent, it's a good idea to "freeze"
163+
the current state of the environment packages. To do this, run
164+
165+
::
166+
167+
pip freeze > requirements.txt
168+
169+
This will create a ``requirements.txt`` file, which contains a simple
170+
list of all the packages in the current environment, and their respective
171+
versions. Later, when a different developer (or you, if you need to re-
172+
create the environment) can install the same packages, with the same
173+
versions by running
174+
175+
::
176+
177+
pip install -r requirements.txt
178+
179+
This can help ensure consistency across installations, across deployments,
180+
and across developers.
181+
182+
Lastly, remember to exclude the virtual environment folder from source
183+
control by adding it to the ignore list.
184+
100185
virtualenvwrapper
101186
-----------------
102187

0 commit comments

Comments
 (0)