forked from sslinux/magepython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonEnvironment.html
More file actions
660 lines (365 loc) · 26.1 KB
/
PythonEnvironment.html
File metadata and controls
660 lines (365 loc) · 26.1 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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
<!DOCTYPE HTML>
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>python编程环境准备 · GitBook</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.2">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="Python快速入门.html" />
<link rel="prev" href="./" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" data-path="./">
<a href="./">
Introduction
</a>
</li>
<li class="chapter active" data-level="1.2" data-path="PythonEnvironment.html">
<a href="PythonEnvironment.html">
python编程环境准备
</a>
</li>
<li class="chapter " data-level="1.3" data-path="Python快速入门.html">
<a href="Python快速入门.html">
Python快速入门
</a>
</li>
<li class="chapter " data-level="1.4" data-path="Python内置类型.html">
<a href="Python内置类型.html">
Python内置类型
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.1" data-path="comyn_lesson/list.html">
<a href="comyn_lesson/list.html">
list
</a>
</li>
<li class="chapter " data-level="1.4.2" data-path="comyn_lesson/tuple.html">
<a href="comyn_lesson/tuple.html">
tuple
</a>
</li>
<li class="chapter " data-level="1.4.3" data-path="comyn_lesson/string.html">
<a href="comyn_lesson/string.html">
str
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5" data-path="字典、表达式和语句.html">
<a href="字典、表达式和语句.html">
字典、表达式和语句
</a>
</li>
<li class="chapter " data-level="1.6" data-path="Python程序控制结构.html">
<a href="Python程序控制结构.html">
Python程序控制结构
</a>
</li>
<li class="chapter " data-level="1.7" data-path="Python迭代器、列表解析及生成器.html">
<a href="Python迭代器、列表解析及生成器.html">
Python迭代器、列表解析及生成器
</a>
</li>
<li class="chapter " data-level="1.8" data-path="Python文件对象及os、os.path和pickle模块.html">
<a href="Python文件对象及os、os.path和pickle模块.html">
Python文件对象及os、os.path和pickle模块
</a>
</li>
<li class="chapter " data-level="1.9" data-path="Python中的正则表达式.html">
<a href="Python中的正则表达式.html">
Python中的正则表达式
</a>
</li>
<li class="chapter " data-level="1.10" data-path="Python函数详解.html">
<a href="Python函数详解.html">
Python函数详解
</a>
</li>
<li class="chapter " data-level="1.11" data-path="Python中的类详解.html">
<a href="Python中的类详解.html">
Python中的类详解
</a>
</li>
<li class="chapter " data-level="1.12" data-path="Python模块和包.html">
<a href="Python模块和包.html">
Python模块和包
</a>
</li>
<li class="chapter " data-level="1.13" data-path="Python异常.html">
<a href="Python异常.html">
Python异常
</a>
</li>
<li class="chapter " data-level="1.14" data-path="Python编程之MySQLdb模块.html">
<a href="Python编程之MySQLdb模块.html">
Python编程之MySQLdb模块
</a>
</li>
<li class="chapter " data-level="1.15" data-path="Python编程之socket模块.html">
<a href="Python编程之socket模块.html">
Python编程之socket模块
</a>
</li>
<li class="chapter " data-level="1.16" data-path="comyn_lesson/SUMMARY.html">
<a href="comyn_lesson/SUMMARY.html">
comyn_lesson
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="." >python编程环境准备</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h1 id="centos-7-打造-python开发环境">Centos 7 打造 Python开发环境</h1>
<p>安装Centos 7,配置网络,配置yum,安装desktop;</p>
<p>关闭firewalld和selinux;</p>
<h2 id="1、安装idea">1、安装IDEA</h2>
<p>下载地址:<a href="https://download.jetbrains.8686c.com/idea/ideaIU-2016-2.4.tar.gz" target="_blank">https://download.jetbrains.8686c.com/idea/ideaIU-2016-2.4.tar.gz</a></p>
<pre><code class="lang-bash">tar xf ideaIU-2016.2.4.tar.gz
ln -sv idea-IU-162.2032.8 idea
<span class="hljs-built_in">cd</span> idea
./bin/idea.sh
<span class="hljs-comment"># 因为我下的版本是包含了JDK的,所以不需要单独安装JDK,否则需要安装并配置JDK;</span>
ln -sv ./bin/idea.sh /usr/bin/idea <span class="hljs-comment"># 为了方便,创建一个软链接;</span>
</code></pre>
<p>上述命令启动了安装界面,顺着界面就可以完成安装了,可以为所有用户创建一个快捷图标;</p>
<p>然后在初始界面点击configure --> 并安装plugins --> python --> Install</p>
<p>安装完成后就可以创建python项目了</p>
<h2 id="2、安装git:">2、安装git:</h2>
<pre><code class="lang-bash">[sslinux@localhost ~]$ sudo yum -y install git
</code></pre>
<p><strong>安装pyenv</strong>:</p>
<p>为了安装pyenv方便,作者开发了另一个项目:pyenv-installer来帮助广大劳苦大众安装pyenv;</p>
<p>pyenv:<a href="https://github.com/yyuu/pyenv/" target="_blank">https://github.com/yyuu/pyenv/</a></p>
<p>pyenv-installer:<a href="https://github.com/yyuu/pyenv-installer" target="_blank">https://github.com/yyuu/pyenv-installer</a></p>
<p>使用pyenv-installer安装pyenv:</p>
<pre><code class="lang-bash">$ curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
</code></pre>
<p>修改环境变量:</p>
<pre><code class="lang-bash">[sslinux@localhost ~]$ vim ~/.bash_profile <span class="hljs-comment"># 仅对当前用户有效;</span>
<span class="hljs-built_in">export</span> PATH=<span class="hljs-string">"/home/sslinux/.pyenv/bin:<span class="hljs-variable">$PATH</span>"</span>
<span class="hljs-built_in">eval</span> <span class="hljs-string">"<span class="hljs-variable">$(pyenv init -)</span>"</span>
<span class="hljs-built_in">eval</span> <span class="hljs-string">"<span class="hljs-variable">$(pyenv virtualenv-init -)</span>"</span>
</code></pre>
<p>所有用户有效:</p>
<pre><code class="lang-bash">[sslinux@localhost ~]$ cat /etc/profile.d/pyenv.sh
<span class="hljs-built_in">export</span> PATH=<span class="hljs-string">"/home/sslinux/.pyenv/bin:<span class="hljs-variable">$PATH</span>"</span>
<span class="hljs-built_in">eval</span> <span class="hljs-string">"<span class="hljs-variable">$(pyenv init -)</span>"</span>
<span class="hljs-built_in">eval</span> <span class="hljs-string">"<span class="hljs-variable">$(pyenv virtualenv-init -)</span>"</span>
</code></pre>
<p>更新pyenv:</p>
<pre><code class="lang-bash">pyenv update
</code></pre>
<p><strong>卸载pyenv</strong>
pyenv 默认安装在环境变量$PYENV_ROOT所指定的目录下(默认为:~/.pyenv)</p>
<pre><code class="lang-bash">$ rm -rf ~/.pyenv
<span class="hljs-comment"># 将配置文件中的相关配置删除掉;</span>
<span class="hljs-built_in">export</span> PATH=<span class="hljs-string">"/home/sslinux/.pyenv/bin:<span class="hljs-variable">$PATH</span>"</span>
<span class="hljs-built_in">eval</span> <span class="hljs-string">"<span class="hljs-variable">$(pyenv init -)</span>"</span>
<span class="hljs-built_in">eval</span> <span class="hljs-string">"<span class="hljs-variable">$(pyenv virtualenv-init -)</span>"</span>
</code></pre>
<h3 id="通过pypi的方式安装pyenv:">通过PyPi的方式安装pyenv:</h3>
<pre><code class="lang-bash">$ pip install --egg pyenv
</code></pre>
<h2 id="3、安装python:">3、安装Python:</h2>
<p><strong>安装编译工具:</strong></p>
<pre><code class="lang-bash">[sslinux@localhost ~]$ sudo yum -y install gcc make patch
</code></pre>
<p><strong>安装依赖包:</strong></p>
<pre><code class="lang-bash">[sslinux@localhost ~]$ sudo yum -y install gdbm-devel openssl-devel \
sqlite-devel readline-devel zlib-devel bzip2-devel
</code></pre>
<p><strong>安装python 3.5.2</strong></p>
<pre><code class="lang-bash">[sslinux@localhost ~]$ pyenv install 3.5.2
</code></pre>
<h3 id="使用pyenv:">使用pyenv:</h3>
<pre><code class="lang-bash">[sslinux@localhost .pyenv]$ pyenv
pyenv 1.0.2
Usage: pyenv <<span class="hljs-built_in">command</span>> [<args>]
Some useful pyenv commands are:
commands List all available pyenv commands <span class="hljs-comment"># 列出所有可用的子命令,包括virtualenv</span>
<span class="hljs-built_in">local</span> Set or show the <span class="hljs-built_in">local</span> application-specific Python version
global Set or show the global Python version
shell Set or show the shell-specific Python version
install Install a Python version using python-build
uninstall Uninstall a specific Python version
<span class="hljs-built_in">rehash</span> Rehash pyenv shims (run this after installing executables)
version Show the current Python version and its origin
versions List all Python versions available to pyenv
<span class="hljs-built_in">which</span> Display the full path to an executable
<span class="hljs-built_in">whence</span> List all Python versions that contain the given executable
See `pyenv <span class="hljs-built_in">help</span> <<span class="hljs-built_in">command</span>>` <span class="hljs-keyword">for</span> information on a specific command.
For full documentation, see: https://github.com/yyuu/pyenv<span class="hljs-comment">#readme</span>
</code></pre>
<p><strong>local命令:</strong> </p>
<p>local命令切换当前目录及子目录的Python版本,可以通过删除'.python-version'恢复默认Python版本;</p>
<p><strong>global命令:</strong></p>
<p>global切换全局默认Python版本,不建议这么使用;</p>
<h4 id="virtualenv命令:">virtualenv命令:</h4>
<p>创建虚拟环境:</p>
<pre><code class="lang-bash">pyenv virtualenv <span class="hljs-variable">$python_version</span> <span class="hljs-variable">$name</span>
</code></pre>
<pre><code class="lang-bash">[sslinux@localhost .pyenv]$ pyenv <span class="hljs-built_in">help</span> virtualenv
Usage: pyenv virtualenv [<span class="hljs-_">-f</span>|--force] [VIRTUALENV_OPTIONS] [version] <virtualenv-name>
pyenv virtualenv --version
pyenv virtualenv --help
<span class="hljs-_">-f</span>/--force Install even <span class="hljs-keyword">if</span> the version appears to be installed already
</code></pre>
<p><strong>install命令:</strong></p>
<p>安装指定版本,后面直接跟版本号即可;</p>
<p><strong>Uninstall命令:</strong></p>
<p>卸载某个版本,包括虚拟环境;</p>
<p><strong>环境配置原理:</strong></p>
<pre><code>python的环境是基于site的,即整个机器的python环境是一样的;
java是基于project的,每个项目可以使用不同的java环境;
</code></pre><h3 id="pyenv-virtualenv-命令:">pyenv virtualenv 命令:</h3>
<pre><code class="lang-bash">[sslinux@localhost ~]$ pyenv virtualenv 3.5.2 py2go <span class="hljs-comment"># 创建一个虚拟环境,名叫py2go</span>
Ignoring indexes: https://pypi.python.org/simple
Requirement already satisfied (use --upgrade to upgrade): setuptools <span class="hljs-keyword">in</span> /home/sslinux/.pyenv/versions/3.5.2/envs/py2go/lib/python3.5/site-packages
Requirement already satisfied (use --upgrade to upgrade): pip <span class="hljs-keyword">in</span> /home/sslinux/.pyenv/versions/3.5.2/envs/py2go/lib/python3.5/site-packages
[sslinux@localhost ~]$ pyenv versions
* system (<span class="hljs-built_in">set</span> by /home/sslinux/.pyenv/version)
3.5.2
3.5.2/envs/py2go
3.5.2/envs/symbio
py2go
symbio
</code></pre>
<p>切换到虚拟环境:</p>
<pre><code class="lang-bash">[sslinux@localhost ~]$ pyenv <span class="hljs-built_in">local</span> py2go <span class="hljs-comment"># 切换本地python环境为指定虚拟环境</span>
(py2go) [sslinux@localhost ~]$ python -V
Python 3.5.2
(py2go) [sslinux@localhost ~]$ pyenv version
py2go (<span class="hljs-built_in">set</span> by /home/sslinux/.python-version)
</code></pre>
<p>所有使用pyenv创建的python版本,都在~/.pyenv/versions/目录下;</p>
<h3 id="python-ide">python IDE</h3>
<ul>
<li>Pycharm</li>
<li>IDEA</li>
<li>Atom</li>
<li>vscode</li>
</ul>
<h2 id="pip命令">pip命令</h2>
<pre><code>[sslinux@localhost ~]$ pyenv versions
* system (set by /home/sslinux/.python-version)
3.5.2
3.5.2/envs/py2go
py2go
[sslinux@localhost ~]$ pyenv local py2go
(py2go) [sslinux@localhost ~]$ mkdir ~/.pip
(py2go) [sslinux@localhost ~]$ vim ~/.pip/pip.conf
(py2go) [sslinux@localhost ~]$ cat ~/.pip/pip.conf # 为提高下载速度设置;
</code></pre><pre><code class="lang-ini"><span class="hljs-section">[global]</span>
<span class="hljs-attr">index-url</span> = http://mirrors.aliyun.com/pypi/simple/
<span class="hljs-attr">trusted-host</span> = mirrors.aliyun.com
</code></pre>
<p>在虚拟环境中,使用pip安装ipython,安装其他模块和包也是一样的;</p>
<pre><code class="lang-bash">(py2go) [sslinux@localhost ~]$ pip install ipython
</code></pre>
<p><strong>pip 安装jupyter:</strong></p>
<p>安装文档:<a href="http://jupyter.org/install.html" target="_blank">http://jupyter.org/install.html</a></p>
<pre><code class="lang-bash">pip3 install --upgrade pip
pip3 install jupyter
</code></pre>
<pre><code class="lang-bash">(py2go) [sslinux@localhost ~]$ jupyter -h
usage: jupyter [-h] [--version] [--config-dir] [--data-dir] [--runtime-dir]
[--paths] [--json]
[subcommand]
Jupyter: Interactive Computing
positional arguments:
subcommand the subcommand to launch
optional arguments:
-h, --help show this <span class="hljs-built_in">help</span> message and <span class="hljs-built_in">exit</span>
--version show the jupyter <span class="hljs-built_in">command</span><span class="hljs-string">'s version and exit
--config-dir show Jupyter config dir
--data-dir show Jupyter data dir
--runtime-dir show Jupyter runtime dir
--paths show all Jupyter paths. Add --json for machine-readable
format.
--json output paths as machine-readable json
Available subcommands: console kernelspec migrate nbconvert nbextension
notebook qtconsole serverextension troubleshoot trust
</span></code></pre>
<p><strong>启动jupyter:</strong></p>
<pre><code class="lang-bash">(py2go) [sslinux@localhost ~]$ jupyter notebook --no-browser --ip=0.0.0.0
</code></pre>
<p>此时便可以在本地的浏览器中使用ip加端口进行访问了;</p>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="./" class="navigation navigation-prev " aria-label="Previous page: Introduction">
<i class="fa fa-angle-left"></i>
</a>
<a href="Python快速入门.html" class="navigation navigation-next " aria-label="Next page: Python快速入门">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"python编程环境准备","level":"1.2","depth":1,"next":{"title":"Python快速入门","level":"1.3","depth":1,"path":"Python快速入门.md","ref":"Python快速入门.md","articles":[]},"previous":{"title":"Introduction","level":"1.1","depth":1,"path":"README.md","ref":"README.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"PythonEnvironment.md","mtime":"2017-02-07T06:09:38.080Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-02-07T10:07:43.025Z"},"basePath":".","book":{"language":""}});
});
</script>
</div>
<script src="gitbook/gitbook.js"></script>
<script src="gitbook/theme.js"></script>
<script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
<script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
<script src="gitbook/gitbook-plugin-search/search.js"></script>
<script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
<script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
<script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
<script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>