-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathvirtualenv.html
More file actions
589 lines (484 loc) · 30.9 KB
/
Copy pathvirtualenv.html
File metadata and controls
589 lines (484 loc) · 30.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Working with Virtualenv — Introduction To Python 1.3 documentation</title>
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="top" title="Introduction To Python 1.3 documentation" href="../index.html"/>
<link rel="up" title="Supplemental Materials" href="index.html"/>
<link rel="next" title="Turning Sublime Text Into a Lightweight Python IDE" href="sublime_as_ide.html"/>
<link rel="prev" title="Setting Up Python For Linux" href="python_for_linux.html"/>
<script src="../_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="../index.html" class="icon icon-home"> Introduction To Python
</a>
<div class="version">
1.3
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../session01.html">Session One: Introductions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../session02.html">Session Two: gitHub, Functions, Booleans and Modules</a></li>
<li class="toctree-l1"><a class="reference internal" href="../session03.html">Session Three: Sequences, Iteration and String Formatting</a></li>
<li class="toctree-l1"><a class="reference internal" href="../session04.html">Session Four: Dictionaries, Sets, and Files</a></li>
<li class="toctree-l1"><a class="reference internal" href="../session05.html">Session Five: Exceptions, Testing, Comprehensions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../session06.html">Session Six: Advanced Argument Passing, lambda, functions as objects</a></li>
<li class="toctree-l1"><a class="reference internal" href="../session07.html">Object Oriented Programming</a></li>
<li class="toctree-l1"><a class="reference internal" href="../session08.html">Session Eight: More OO: Properties, Special methods.</a></li>
<li class="toctree-l1"><a class="reference internal" href="../session09.html">Session Nine: Iterators, Iterables, and Generators</a></li>
<li class="toctree-l1"><a class="reference internal" href="../session10.html">Session Ten: Decorators and Context Managers – Wrap Up</a></li>
</ul>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../exercises/index.html">Exercises</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Supplemental Materials</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="python_learning_resources.html">Useful Python Learning Resources</a></li>
<li class="toctree-l2"><a class="reference internal" href="python_for_mac.html">Setting up your Mac for Python</a></li>
<li class="toctree-l2"><a class="reference internal" href="python_for_windows.html">Setting up Windows for Python</a></li>
<li class="toctree-l2"><a class="reference internal" href="python_for_linux.html">Setting Up Python For Linux</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="">Working with Virtualenv</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#reasons-why">Reasons Why</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#installing-virtualenv">Installing Virtualenv</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#using-virtualenv">Using Virtualenv</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#what-happened">What Happened?</a></li>
<li class="toctree-l4"><a class="reference internal" href="#activation">Activation</a></li>
<li class="toctree-l4"><a class="reference internal" href="#installing-packages">Installing Packages</a></li>
<li class="toctree-l4"><a class="reference internal" href="#deactivation">Deactivation</a></li>
<li class="toctree-l4"><a class="reference internal" href="#cleaning-up">Cleaning Up</a></li>
<li class="toctree-l4"><a class="reference internal" href="#installation">Installation</a></li>
<li class="toctree-l4"><a class="reference internal" href="#using-mkvirtualenv">Using <code class="docutils literal"><span class="pre">mkvirtualenv</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="#using-workon">Using <code class="docutils literal"><span class="pre">workon</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="#using-mkproject">Using <code class="docutils literal"><span class="pre">mkproject</span></code></a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="sublime_as_ide.html">Turning Sublime Text Into a Lightweight Python IDE</a></li>
<li class="toctree-l2"><a class="reference internal" href="shell.html">Shell Customizations for Python Development</a></li>
<li class="toctree-l2"><a class="reference internal" href="git_overview.html">git Overview</a></li>
<li class="toctree-l2"><a class="reference internal" href="install_nano_win.html">Installing Nano on Windows</a></li>
<li class="toctree-l2"><a class="reference internal" href="unicode.html">Unicode</a></li>
<li class="toctree-l2"><a class="reference internal" href="unicode.html#mechanics">Mechanics</a></li>
<li class="toctree-l2"><a class="reference internal" href="unicode.html#basic-unicode-lab">Basic Unicode LAB</a></li>
<li class="toctree-l2"><a class="reference internal" href="packaging.html">Packages and Packaging</a></li>
<li class="toctree-l2"><a class="reference internal" href="packaging.html#distributing">Distributing</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">Introduction To Python</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html">Docs</a> »</li>
<li><a href="index.html">Supplemental Materials</a> »</li>
<li>Working with Virtualenv</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/supplements/virtualenv.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="working-with-virtualenv">
<span id="virtualenv-section"></span><h1>Working with Virtualenv<a class="headerlink" href="#working-with-virtualenv" title="Permalink to this headline">¶</a></h1>
<p class="medium">“For every non-standard package installed in a system Python, the gods kill a
kitten”</p>
<ul class="medium simple">
<li>me</li>
</ul>
<div class="section" id="reasons-why">
<h2>Reasons Why<a class="headerlink" href="#reasons-why" title="Permalink to this headline">¶</a></h2>
<ul class="left simple">
<li>As a working developer you will need to install packages that aren’t in the
Python standard Library</li>
<li>As a working developer you often need to install <em>different</em> versions of the
<em>same</em> library for different projects</li>
<li>Conflicts arising from having the wrong version of a dependency installed can
cause long-term nightmares</li>
<li>Use <a class="reference external" href="http://www.virtualenv.org/">virtualenv</a> ...</li>
<li><strong>Always</strong></li>
</ul>
<div class="section" id="installing-virtualenv">
<h3>Installing Virtualenv<a class="headerlink" href="#installing-virtualenv" title="Permalink to this headline">¶</a></h3>
<p>The best way is to install directly in your system Python (one exception to the
rule).</p>
<p>To do so you will have to have <a class="reference external" href="http://www.pip-installer.org">pip</a> installed.</p>
<p>Try the following command:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>which pip
/usr/local/bin/pip
</pre></div>
</div>
<p>If the <code class="docutils literal"><span class="pre">which</span></code> command returns no value for you, then <code class="docutils literal"><span class="pre">pip</span></code> is not
installed in your system. To fix this, follow <a class="reference external" href="http://www.pip-installer.org/en/latest/installing.html">the instructions here</a>.</p>
<p>Once you have <code class="docutils literal"><span class="pre">pip</span></code> installed in your system, you can use it to install
<a class="reference external" href="http://www.virtualenv.org/">virtualenv</a>. Because you are installing it into your system python, you will
most likely need <code class="docutils literal"><span class="pre">superuser</span></code> privileges to do so:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>sudo pip install virtualenv
Downloading/unpacking virtualenv
Downloading virtualenv-1.11.2-py2.py3-none-any.whl <span class="o">(</span>2.8MB<span class="o">)</span>: 2.8MB downloaded
Installing collected packages: virtualenv
Successfully installed virtualenv
Cleaning up...
</pre></div>
</div>
<p>Great. Once that’s done, you should find that you have a <code class="docutils literal"><span class="pre">virtualenv</span></code>
command available to you from your shell:</p>
<div class="highlight-bash"><div class="highlight"><pre>$ virtualenv --help
Usage: virtualenv [OPTIONS] DEST_DIR
Options:
--version show program's version number and exit
-h, --help ...
</pre></div>
</div>
</div>
</div>
<div class="section" id="using-virtualenv">
<h2>Using Virtualenv<a class="headerlink" href="#using-virtualenv" title="Permalink to this headline">¶</a></h2>
<p class="left">Creating a new virtualenv is very very simple:</p>
<div class="left highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>virtualenv <span class="o">[</span>options<span class="o">]</span> <ENV>
</pre></div>
</div>
<p class="left"><code class="docutils literal"><span class="pre"><ENV></span></code> is just the name of the environment you want to create. It’s
arbitrary. Let’s make one for demonstration purposes:</p>
<div class="left highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>virtualenv demoenv
New python executable in demoenv/bin/python
Installing setuptools, pip...done.
</pre></div>
</div>
<div class="section" id="what-happened">
<h3>What Happened?<a class="headerlink" href="#what-happened" title="Permalink to this headline">¶</a></h3>
<p>When you ran that command, a couple of things took place:</p>
<ul class="simple">
<li>A new directory with your requested name was created</li>
<li>A new Python executable was created in <ENV>/bin (<ENV>/Scripts on Windows)</li>
<li>The new Python was cloned from your system Python (where virtualenv was
installed)</li>
<li>The new Python was isolated from any libraries installed in the old Python</li>
<li>Setuptools was installed so you have <code class="docutils literal"><span class="pre">easy_install</span></code> for this new python</li>
<li>Pip was installed so you have <code class="docutils literal"><span class="pre">pip</span></code> for this new python</li>
</ul>
</div>
<div class="section" id="activation">
<h3>Activation<a class="headerlink" href="#activation" title="Permalink to this headline">¶</a></h3>
<p>The virtual environment you just created, <code class="docutils literal"><span class="pre">demoenv</span></code> contains an executable
Python command, but if you do a quick check to see which Python executable is
found by your terminal, you’ll see that it is not the one:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>which python
/usr/bin/python
</pre></div>
</div>
<p>You can execute the new Python by explicitly pointing to it:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>./demoenv/bin/python -V
Python 2.7.5
</pre></div>
</div>
<p>but that’s tedious and hard to remember. Instead, <code class="docutils literal"><span class="pre">activate</span></code> your virtualenv
using the <code class="docutils literal"><span class="pre">source</span></code> command:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span><span class="nb">source </span>demoenv/bin/activate
<span class="o">(</span>demoenv<span class="o">)</span><span class="nv">$ </span>which python
/Users/cewing/demoenv/bin/python
</pre></div>
</div>
<p>There. That’s better. Now whenever you run the <code class="docutils literal"><span class="pre">python</span></code> command, the
executable that will be used will be the new one in your <code class="docutils literal"><span class="pre">demoenv</span></code>.</p>
<p>Notice also that the your shell prompt has changed. It indicates which
<code class="docutils literal"><span class="pre">virtualenv</span></code> is currently active. Little clues like that really help you to
keep things straight when you’ve got a lot of projects going on, so it’s nice
the makers of virtualenv thought of it.</p>
</div>
<div class="section" id="installing-packages">
<h3>Installing Packages<a class="headerlink" href="#installing-packages" title="Permalink to this headline">¶</a></h3>
<p>Now that your virtualenv is active, not only has your <code class="docutils literal"><span class="pre">python</span></code> executable been
hijacked, so have <code class="docutils literal"><span class="pre">pip</span></code> and <code class="docutils literal"><span class="pre">easy_install</span></code>:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="o">(</span>demoenv<span class="o">)</span><span class="nv">$ </span>which pip
/Users/cewing/demoenv/bin/pip
<span class="o">(</span>demoenv<span class="o">)</span><span class="nv">$ </span>which easy_install
/Users/cewing/demoenv/bin/easy_install
</pre></div>
</div>
<p>This means that using these tools to install packages will install them <em>into
your virtual environment only</em> and not into the system Python. Let’s see this
in action. We’ll install a package called <code class="docutils literal"><span class="pre">docutils</span></code> that provides support
for converting ReStructuredText documents into other formats like HTML, LaTeX
and more:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="o">(</span>demoenv<span class="o">)</span><span class="nv">$ </span>pip install docutils
Downloading/unpacking docutils
Downloading docutils-0.11.tar.gz <span class="o">(</span>1.6MB<span class="o">)</span>: 1.6MB downloaded
Running setup.py <span class="o">(</span>path:/Users/cewing/demoenv/build/docutils/setup.py<span class="o">)</span> egg_info <span class="k">for</span> package docutils
...
changing mode of /Users/cewing/demoenv/bin/rst2xml.py to 755
changing mode of /Users/cewing/demoenv/bin/rstpep2html.py to 755
Successfully installed docutils
Cleaning up...
</pre></div>
</div>
<p>And now, when we fire up our Python interpreter, the docutils package is
available to us:</p>
<div class="highlight-pycon"><div class="highlight"><pre><span class="go">(demoenv)$ python</span>
<span class="go">Python 2.7.5 (default, Aug 25 2013, 00:04:04)</span>
<span class="go">[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin</span>
<span class="go">Type "help", "copyright", "credits" or "license" for more information.</span>
<span class="gp">>>> </span><span class="kn">import</span> <span class="nn">docutils</span>
<span class="gp">>>> </span><span class="n">docutils</span><span class="o">.</span><span class="n">__path__</span>
<span class="go">['/Users/cewing/demoenv/lib/python2.7/site-packages/docutils']</span>
<span class="gp">>>> </span><span class="o">^</span><span class="n">d</span>
<span class="go">(demoenv)$</span>
</pre></div>
</div>
<p>There’s one other interesting side-effect of installing software with
<code class="docutils literal"><span class="pre">virtualenv</span></code>. The <code class="docutils literal"><span class="pre">docutils</span></code> package provides a number of executable
scripts when it is installed: <code class="docutils literal"><span class="pre">rst2html.py</span></code>, <code class="docutils literal"><span class="pre">rst2latex.py</span></code> and so on.
These scripts are set up to execute using the Python with which they were
built. What this means is that running these scripts will use the Python
executable in your virtualenv, <em>even if that virtualenv is not active</em>!</p>
</div>
<div class="section" id="deactivation">
<h3>Deactivation<a class="headerlink" href="#deactivation" title="Permalink to this headline">¶</a></h3>
<p>So you’ve got a virtual environment created. And you’ve activated it so that
you can install packages and use them. Eventually you’ll need to move on to
some other project. This likely means that you’ll need to stop working with
this <code class="docutils literal"><span class="pre">virtualenv</span></code> and switch to another (it’s a good idea to keep a separate
<code class="docutils literal"><span class="pre">virtualenv</span></code> for every project you work on).</p>
<p>When a <code class="docutils literal"><span class="pre">virtualenv</span></code> is active, all you have to do is use the <code class="docutils literal"><span class="pre">deactivate</span></code>
command:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="o">(</span>demoenv<span class="o">)</span><span class="nv">$ </span>deactivate
<span class="nv">$ </span>which python
/usr/bin/python
</pre></div>
</div>
<p>Note that your shell prompt returns to normal, and now the executable Python
found when you check <code class="docutils literal"><span class="pre">python</span></code> is the system one again.</p>
</div>
<div class="section" id="cleaning-up">
<h3>Cleaning Up<a class="headerlink" href="#cleaning-up" title="Permalink to this headline">¶</a></h3>
<p>The final great advantage that <code class="docutils literal"><span class="pre">virtualenv</span></code> confers on you as a developer is
the ability to easily remove a batch of installed Python software from your
system. Consider a situation where you installed a library that breaks your
Python (it happens). If you are working in your system Python, you now have to
figure out what that package installed, where, and go clean it out manually.
With <code class="docutils literal"><span class="pre">virtualenv</span></code> the process is as simple as removing the directory that
virtualenv created when you started out. Let’s do that with our <code class="docutils literal"><span class="pre">demoenv</span></code>:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>rm -rf demoenv
</pre></div>
</div>
<p>And that’s it. The entire environment and all the packages you installed into
it are now gone. There’s no traces left to pollute your world.</p>
<div class="section" id="virtualenvwrapper">
<h4>VirtualenvWrapper<a class="headerlink" href="#virtualenvwrapper" title="Permalink to this headline">¶</a></h4>
<p>So you have this great tool that allows you to build isolated environments in
which you can install Python software. Several questions arise when considering
this.</p>
<ul class="simple">
<li>Where should such environments be placed?</li>
<li>How can the environments be tied to the projects you are working on?</li>
<li>Once you have more than a trivial number of projects, how can you keep track
of all these virtualenvs?</li>
</ul>
<p>Like any good tool, <code class="docutils literal"><span class="pre">virtualenv</span></code> does not impose on you any particular way of
working. You can place your environments into the directories where you are
building the project to which they apply. You can keep them all in a single
global location. You can build a random path generator that drops them
wherever.</p>
<p>But any of these methods lead inevetably to chaos. They require too much from
you. It would be better if you could manage your virtual environments easily
and intuitively.</p>
<p>With <a class="reference external" href="http://virtualenvwrapper.readthedocs.org">virtualenvwrapper</a> you can.</p>
</div>
</div>
<div class="section" id="installation">
<h3>Installation<a class="headerlink" href="#installation" title="Permalink to this headline">¶</a></h3>
<p>Let’s start by installing the package in our system Python, alongside
<code class="docutils literal"><span class="pre">virtualenv</span></code> (again, you’ll need <code class="docutils literal"><span class="pre">superuser</span></code> to do this):</p>
<div class="highlight-bash"><div class="highlight"><pre>$ sudo pip install virtualenvwrapper
Downloading/unpacking virtualenvwrapper
Downloading virtualenvwrapper-4.2.tar.gz (125kB): 125kB downloaded
Running setup.py (path:/private/tmp/pip_build_root/virtualenvwrapper/setup.py) egg_info for package virtualenvwrapper
...
Successfully installed virtualenvwrapper virtualenv-clone stevedore
Cleaning up...
$
</pre></div>
</div>
<p>Once that’s finished, you’ll need to wire the system up by letting your shell
know that the commands it provides are present. Add the following lines to your
shell startup file (<code class="docutils literal"><span class="pre">.profile</span></code>, <code class="docutils literal"><span class="pre">.bash-profile</span></code>, ...):</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nb">export </span><span class="nv">WORKON_HOME</span><span class="o">=</span>~/.virtualenvs
<span class="nb">source</span> /usr/local/bin/virtualenvwrapper.sh
</pre></div>
</div>
<p>This will create a new environmental variable, <code class="docutils literal"><span class="pre">WORKON_HOME</span></code>, that determines
where new virtual environments will be created. The actual name is completely
arbitrary.</p>
<p>You’ll need to be sure that the location you set exists:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>mkdir ~/.virtualenvs
</pre></div>
</div>
</div>
<div class="section" id="using-mkvirtualenv">
<h3>Using <code class="docutils literal"><span class="pre">mkvirtualenv</span></code><a class="headerlink" href="#using-mkvirtualenv" title="Permalink to this headline">¶</a></h3>
<p>When you’ve done that, start a new terminal and you’ll have access to the
<code class="docutils literal"><span class="pre">mkvirtualenv</span></code> command:</p>
<div class="highlight-bash"><div class="highlight"><pre>$ mkvirtualenv testenv
New python executable in testenv/bin/python
Installing setuptools, pip...done.
(testenv)$ ls ~/.virtualenvs
testenv
(testenv)$ which python
/Users/cewing/.virtualenvs/testenv/bin/python
(testenv)$
</pre></div>
</div>
<p>Notice a couple of things:</p>
<ul class="simple">
<li>The new environment you asked for was created in <code class="docutils literal"><span class="pre">WORKON_HOME</span></code></li>
<li>The new environment was <em>immedately</em> activated for you</li>
</ul>
<p>That’s a nice feature, eh? No more needing to remember to <code class="docutils literal"><span class="pre">activate</span></code> the env
you just created to install packages.</p>
</div>
<div class="section" id="using-workon">
<h3>Using <code class="docutils literal"><span class="pre">workon</span></code><a class="headerlink" href="#using-workon" title="Permalink to this headline">¶</a></h3>
<p>In addition to this nice little feature, you can also use the <code class="docutils literal"><span class="pre">workon</span></code>
command to see which environments you have, and to switch from one to another:</p>
<div class="highlight-bash"><div class="highlight"><pre>(testenv)$ workon
testenv
(testenv)$ mkvirtualenv number2
New python executable in number2/bin/python
Installing setuptools, pip...done.
(number2)$ workon
number2
testenv
(number2)$ workon testenv
(testenv)$
</pre></div>
</div>
<p>Sweet!</p>
<p>The same <code class="docutils literal"><span class="pre">deactivate</span></code> command can get you back to your system environment:</p>
<div class="highlight-bash"><div class="highlight"><pre>(testenv)$ deactivate
$
</pre></div>
</div>
</div>
<div class="section" id="using-mkproject">
<h3>Using <code class="docutils literal"><span class="pre">mkproject</span></code><a class="headerlink" href="#using-mkproject" title="Permalink to this headline">¶</a></h3>
<p>That takes care of deciding where to put new environments. It also clears up
the question of how to remember which ones you have and how to start them up
and switch between them. But we still have to figure out how to remember which
environment goes with which project.</p>
<p>That’s what the <code class="docutils literal"><span class="pre">mkproject</span></code> command is for.</p>
<p>First, go back to your shell startup file and add a new environmental variable:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nb">export </span><span class="nv">PROJECT_HOME</span><span class="o">=</span>~/projects <span class="c">#<- this line here is new</span>
<span class="nb">export </span><span class="nv">WORKON_HOME</span><span class="o">=</span>~/.virtualenvs
<span class="nb">source</span> /usr/local/bin/virtualenvwrapper.sh
</pre></div>
</div>
<p>Then, make sure the directory you named exists:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>mkdir ~/projects
</pre></div>
</div>
<p>After all that, fire up a new shell to pick up the changes and try this:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>mkproject foo
New python executable in foo/bin/python
Installing setuptools, pip...done.
Creating /Users/cewing/projects/foo
Setting project <span class="k">for</span> foo to /Users/cewing/projects/foo
<span class="o">(</span>foo<span class="o">)</span><span class="nv">$ </span>which python
/Users/cewing/.virtualenvs/foo/bin/python
<span class="o">(</span>foo<span class="o">)</span><span class="nv">$ </span><span class="nb">pwd</span>
/Users/cewing/projects/foo
<span class="o">(</span>foo<span class="o">)</span><span class="nv">$ </span>ls -a <span class="nv">$VIRTUAL_ENV</span>
. .Python bin lib
.. .project include
<span class="o">(</span>foo<span class="o">)</span><span class="nv">$ </span>more <span class="nv">$VIRTUAL_ENV</span>/.project
/Users/cewing/projects/foo
</pre></div>
</div>
<p>Whoa! That command did a lot:</p>
<ul class="simple">
<li>Created a new <code class="docutils literal"><span class="pre">virtualenv</span></code> in your <code class="docutils literal"><span class="pre">$WORKON_HOME</span></code></li>
<li>Created a new project directory in your <code class="docutils literal"><span class="pre">$PROJECT_HOME</span></code></li>
<li>Placed a <code class="docutils literal"><span class="pre">.project</span></code> file in your home directory with a path leading to the
associated project directory</li>
<li>Activated the new virtualenv for you</li>
<li>Automatically moved your present working directory to the new project
directory.</li>
</ul>
<p>And now, you can begin working on your <code class="docutils literal"><span class="pre">foo</span></code> project, secure that you will be
installing packages into the right environment.</p>
<div class="section" id="a-few-last-words">
<h4>A Few Last Words<a class="headerlink" href="#a-few-last-words" title="Permalink to this headline">¶</a></h4>
<p>This quick introduction is <strong>by no means</strong> an exhaustive manual for either of
the packages we’ve talked about. There is a great deal more that they can do.
In particular, <code class="docutils literal"><span class="pre">virtualenvwrapper</span></code> is highly customizable, with support for
custom scripts to be hooked into every stage of the <code class="docutils literal"><span class="pre">virtualenv</span></code> workflow.</p>
<p>I urge you to read the documentation for <a class="reference external" href="http://www.virtualenv.org/">virtualenv</a> and <a class="reference external" href="http://virtualenvwrapper.readthedocs.org">virtualenvwrapper</a>
yourself to find out more.</p>
</div>
</div>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="sublime_as_ide.html" class="btn btn-neutral float-right" title="Turning Sublime Text Into a Lightweight Python IDE" accesskey="n">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="python_for_linux.html" class="btn btn-neutral" title="Setting Up Python For Linux" accesskey="p"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
© Copyright 2014, Christopher Barker, Cris Ewing, .
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'../',
VERSION:'1.3',
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/javascript" src="../_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
</body>
</html>