-
Notifications
You must be signed in to change notification settings - Fork 0
/
describe-pythonvirtualenv-pip-ji-qiao-here.html
334 lines (308 loc) · 13.6 KB
/
describe-pythonvirtualenv-pip-ji-qiao-here.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Describe python/virtualenv pip 技巧 here.</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="liugehao">
<!-- Le styles -->
<link rel="stylesheet" href="http://liugehao.github.io/theme/css/bootstrap.min.css" type="text/css" />
<style type="text/css">
body {
padding-top: 60px;
padding-bottom: 40px;
}
.sidebar-nav {
padding: 9px 0;
}
.tag-1 {
font-size: 13pt;
}
.tag-2 {
font-size: 10pt;
}
.tag-2 {
font-size: 8pt;
}
.tag-4 {
font-size: 6pt;
}
</style>
<link href="http://liugehao.github.io/theme/css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="http://liugehao.github.io/theme/css/font-awesome.css" rel="stylesheet">
<link href="http://liugehao.github.io/theme/css/pygments.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- Le fav and touch icons -->
<link rel="shortcut icon" href="http://liugehao.github.io/theme/images/favicon.ico">
<link rel="apple-touch-icon" href="http://liugehao.github.io/theme/images/apple-touch-icon.png">
<link rel="apple-touch-icon" sizes="72x72" href="http://liugehao.github.io/theme/images/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="114x114" href="http://liugehao.github.io/theme/images/apple-touch-icon-114x114.png">
<link href="http://liugehao.github.io/" type="application/atom+xml" rel="alternate" title="My Note Book ATOM Feed" />
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container-fluid">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="http://liugehao.github.io/index.html">My Note Book </a>
<div class="nav-collapse">
<ul class="nav">
<li class="divider-vertical"></li>
<li >
<a href="http://liugehao.github.io/category/c.html">
<i class="icon-folder-open icon-large"></i>c
</a>
</li>
<li >
<a href="http://liugehao.github.io/category/db.html">
<i class="icon-folder-open icon-large"></i>db
</a>
</li>
<li >
<a href="http://liugehao.github.io/category/linux.html">
<i class="icon-folder-open icon-large"></i>linux
</a>
</li>
<li >
<a href="http://liugehao.github.io/category/linux2fe697a0e7babfe7bd91e58da1make3a202a2a2a2flib.html">
<i class="icon-folder-open icon-large"></i>linux(2fe697a0e7babfe7bd91e58da1)make(3a202a2a2a2f)lib
</a>
</li>
<li >
<a href="http://liugehao.github.io/category/nginx.html">
<i class="icon-folder-open icon-large"></i>nginx
</a>
</li>
<li >
<a href="http://liugehao.github.io/category/others.html">
<i class="icon-folder-open icon-large"></i>Others
</a>
</li>
<li >
<a href="http://liugehao.github.io/category/php.html">
<i class="icon-folder-open icon-large"></i>php
</a>
</li>
<li class="active">
<a href="http://liugehao.github.io/category/python.html">
<i class="icon-folder-open icon-large"></i>python
</a>
</li>
<li >
<a href="http://liugehao.github.io/category/ruby.html">
<i class="icon-folder-open icon-large"></i>ruby
</a>
</li>
<li >
<a href="http://liugehao.github.io/category/tmp.html">
<i class="icon-folder-open icon-large"></i>tmp
</a>
</li>
<ul class="nav pull-right">
<li><a href="http://liugehao.github.io/archives.html"><i class="icon-th-list"></i>Archives</a></li>
</ul>
</ul>
<!--<p class="navbar-text pull-right">Logged in as <a href="#">username</a></p>-->
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="span9" id="content">
<section id="content">
<article>
<header>
<h1>
<a href=""
rel="bookmark"
title="Permalink to Describe python/virtualenv pip 技巧 here.">
Describe python/virtualenv pip 技巧 here.
</a>
</h1>
</header>
<div class="entry-content">
<div class="well">
<footer class="post-info">
<span class="label">Date</span>
<abbr class="published" title="2013-09-04T00:00:00">
<i class="icon-calendar"></i>三 04 九月 2013
</abbr>
<span class="label">By</span>
<a href="http://liugehao.github.io/author/liugehao.html"><i class="icon-user"></i>liugehao</a>
<span class="label">Category</span>
<a href="http://liugehao.github.io/category/python.html"><i class="icon-folder-open"></i>python</a>.
</footer><!-- /.post-info --> </div>
<p>== virtualenv ==
这里是导言吗?
用过Python的同学,肯定会对Python及程序的版本之间经常更换的api感到痛苦不以。就拿我折腾的Django来说吧,公司服务器上跑的是Django1.3、同事也是用1.3开发,但是因为我是新来,一个
pip install django
下去,就是1.4.2。好了,你自己写的Django Project自然没有问题,自己本地测试也没有问题。但是要和其他人交流的时候就蛋疼了,因为你的1.4.2跑不了1.3的程序……当然,这时,你可以选择卸载自己本地的Django,换成1.3,等你要重新测试自己的Django,怎么样,扯着蛋了吧。为了解决以上问题,virtualenv横空出世了。
正文
为了解决以上蛋疼问题,我们需要安装virtualenv。
{{{
sudo pip install virtualenv
}}}
安装好了以后,就可以在任何目录下新建一个virtual-environment(我更喜欢叫:盗梦空间),当然一般我习惯在项目的边上创建一个$project_name-env。例如:
{{{
virtualenv demo-env
}}}
这下,只需要
{{{
source demo-env/bin/activate
}}}
就可以激活这个虚拟环境了。如下图所示:</p>
<p>注意到括号里的(demo-env)了没?对,我们已经进入了第一层梦境demo-env。
回到导言里的问题,我们需要在这里建立一个使用django1.3的环境,不让它干扰我们的本地环境。</p>
<p>{{{
pip install Django==1.3
}}}
就可以了,然后在里面运行别人的Django程序,没有问题!装很多python程序,没有问题,这些都不会干扰你的本地环境,放心破坏吧!</p>
<p>== PIP ==
刚才的virtualenv的介绍里已经说了pip的安装命令了。但你当pip只是个安装工具的话,那就大错特错了。pip还有一个更重要的功能——冻结当前梦境开发环境版本(freeze)。
{{{
(env1)l@x201i:~/pyproject/yuer$ pip freeze
argparse==1.2.1
bottle==0.11.6
bottle-sqlalchemy==0.4
psycopg2==2.5.1
wsgiref==0.1.2</p>
<p>}}}
看到了刚才安装的django 1.3了没有?这就是我们目前的生产环境,把这输出结果写到requirement.txt里,别的程序猿们就知道你的程序至少在这个环境下可以跑,这时他们拿着requirement.txt,新建个virtualenv,
{{{
pip install -r requirement.txt
}}}
就可以运行你的程序了。
pip加速安装
通过pip安装程序,还可以用git和svn的方式,具体操作网上也有。但是对于懒人来说,直接用pip是最好不过的了,但是,有时这会很慢!!!慢到你抓狂。当然,和apt-get一样,pip也可以换源,或者叫添加镜像。
新建一个~/.pip/pip.conf,往里面写入
{{{
[global]
index-url=http://e.pypi.python.org/simple
timeout = 30
require-virtualenv = true
download-cache = /tmp</p>
<p>[install]
use-mirrors = true
mirrors =</p>
<p>http://d.pypi.python.org</p>
<p>http://b.pypi.python.org</p>
<p>http://c.pypi.python.org</p>
<p>}}}
下载应该就会像飞了一样,配置文件过于清晰,所以我也不用多说了吧 :)</p>
</div><!-- /.entry-content -->
<div class="comments">
<h2>Comments !</h2>
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_identifier = "describe-pythonvirtualenv-pip-ji-qiao-here.html";
(function() {
var dsq = document.createElement('script');
dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://liugehaonotebook.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] ||
document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
</div>
</article>
</section>
</div><!--/span-->
<div class="span3 well sidebar-nav" id="sidebar">
<ul class="nav nav-list">
<li class="nav-header"><h4><i class="icon-external-link"></i>blogroll</h4></li>
<li><a href="http://getpelican.com/"><i class="icon-external-link"></i>Pelican</a></li>
<li><a href="http://python.org/"><i class="icon-external-link"></i>Python.org</a></li>
<li><a href="http://jinja.pocoo.org/"><i class="icon-external-link"></i>Jinja2</a></li>
<li><a href="#"><i class="icon-external-link"></i>You can modify those links in your config file</a></li>
<li class="nav-header"><h4><i class="icon-home icon-large"></i> social</h4></li>
<li><a href="http://liugehao.github.io/" rel="alternate"><i class="icon-bookmark icon-large"></i>atom feed</a></li>
<li><a href="#"><i class="icon-You can add links in your config file-sign icon-large"></i>You can add links in your config file</a></li>
<li><a href="#"><i class="icon-Another social link-sign icon-large"></i>Another social link</a></li>
<li class="nav-header"><h4><i class="icon-folder-close icon-large"></i>Categories</h4></li>
<li>
<a href="http://liugehao.github.io/category/c.html">
<i class="icon-folder-open icon-large"></i>c
</a>
</li>
<li>
<a href="http://liugehao.github.io/category/db.html">
<i class="icon-folder-open icon-large"></i>db
</a>
</li>
<li>
<a href="http://liugehao.github.io/category/linux.html">
<i class="icon-folder-open icon-large"></i>linux
</a>
</li>
<li>
<a href="http://liugehao.github.io/category/linux2fe697a0e7babfe7bd91e58da1make3a202a2a2a2flib.html">
<i class="icon-folder-open icon-large"></i>linux(2fe697a0e7babfe7bd91e58da1)make(3a202a2a2a2f)lib
</a>
</li>
<li>
<a href="http://liugehao.github.io/category/nginx.html">
<i class="icon-folder-open icon-large"></i>nginx
</a>
</li>
<li>
<a href="http://liugehao.github.io/category/others.html">
<i class="icon-folder-open icon-large"></i>Others
</a>
</li>
<li>
<a href="http://liugehao.github.io/category/php.html">
<i class="icon-folder-open icon-large"></i>php
</a>
</li>
<li>
<a href="http://liugehao.github.io/category/python.html">
<i class="icon-folder-open icon-large"></i>python
</a>
</li>
<li>
<a href="http://liugehao.github.io/category/ruby.html">
<i class="icon-folder-open icon-large"></i>ruby
</a>
</li>
<li>
<a href="http://liugehao.github.io/category/tmp.html">
<i class="icon-folder-open icon-large"></i>tmp
</a>
</li>
<li class="nav-header"><h4><i class="icon-tags icon-large"></i>Tags</h4></li>
</ul> </div><!--/.well -->
</div><!--/row-->
<hr>
<footer>
<address id="about">
Proudly powered by <a href="http://pelican.notmyidea.org/">Pelican <i class="icon-external-link"></i></a>,
which takes great advantage of <a href="http://python.org">Python <i class="icon-external-link"></i></a>.
</address><!-- /#about -->
<p>The theme is from <a href="http://twitter.github.com/bootstrap/">Bootstrap from Twitter <i class="icon-external-link"></i></a>,
and <a href="http://fortawesome.github.com/Font-Awesome/">Font-Awesome <i class="icon-external-link"></i></a>, thanks!</p>
</footer>
</div><!--/.fluid-container-->
<script type="text/javascript">
var disqus_shortname = 'liugehaonotebook';
(function () {
var s = document.createElement('script'); s.async = true;
s.type = 'text/javascript';
s.src = 'http://' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
</script>
<!-- Le javascript -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="http://liugehao.github.io/theme/js/jquery-1.7.2.min.js"></script>
<script src="http://liugehao.github.io/theme/js/bootstrap.min.js"></script>
</body>
</html>