-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathInstalling_Python.html
More file actions
325 lines (207 loc) · 19.7 KB
/
Installing_Python.html
File metadata and controls
325 lines (207 loc) · 19.7 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
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="zh-CN" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh-CN" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta content="Topic: Installing Python with Anaconda, Difficulty: Easy, Category: Tutorial" name="description" />
<meta content="python, anaconda, instructions, environments, beginner, data science, introduction" name="keywords" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>安装Python — Python Like You Mean It</title>
<script type="text/javascript" src="../_static/js/modernizr.min.js"></script>
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="https://www.googletagmanager.com/gtag/js?id=UA-115029372-1"></script>
<script src="../_static/gtag.js"></script>
<script src="../_static/translations.js"></script>
<script crossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js"></script>
<script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["$", "$"], ["\\(", "\\)"]], "processEscapes": true, "ignoreClass": "document", "processClass": "math|output_area"}})</script>
<script type="text/javascript" src="../_static/js/theme.js"></script>
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/my_theme.css" type="text/css" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="next" title="非正式的Python介绍" href="Informal_Intro_Python.html" />
<link rel="prev" title="介绍Python编程语言" href="GettingStartedWithPython.html" />
</head>
<body class="wy-body-for-nav">
<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"> Python Like You Mean It
</a>
<div class="version">
1.4
</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">
<p class="caption"><span class="caption-text">目录:</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../intro.html">Python Like You Mean It</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../module_1.html">模组1:Python入门</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="SiteFormatting.html">代码格式的简单指南</a></li>
<li class="toctree-l2"><a class="reference internal" href="GettingStartedWithPython.html">介绍Python编程语言</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">安装Python</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#安装Anaconda">安装Anaconda</a></li>
<li class="toctree-l3"><a class="reference internal" href="#我刚对我的电脑做了什么?">我刚对我的电脑做了什么?</a></li>
<li class="toctree-l3"><a class="reference internal" href="#快速入门Conda环境">快速入门Conda环境</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="Informal_Intro_Python.html">非正式的Python介绍</a></li>
<li class="toctree-l2"><a class="reference internal" href="Jupyter_Notebooks.html">Jupyter记事本</a></li>
<li class="toctree-l2"><a class="reference internal" href="Getting_Started_With_IDEs_and_Notebooks.html">设置开发环境</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../module_2.html">模组2:Python基础</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_2_problems.html">模组2:题目</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_3.html">模组3:NumPy基础</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_3_problems.html">模组3:题目</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_4.html">模组4:面向对象编程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_5.html">模组5:琐碎话题</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">Python Like You Mean It</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="../module_1.html">模组1:Python入门</a> »</li>
<li>安装Python</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/Module1_GettingStartedWithPython/Installing_Python.md.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">
<style>
/* CSS overrides for sphinx_rtd_theme */
/* 24px margin */
.nbinput.nblast.container,
.nboutput.nblast.container {
margin-bottom: 19px; /* padding has already 5px */
}
/* ... except between code cells! */
.nblast.container + .nbinput.container {
margin-top: -19px;
}
.admonition > p:before {
margin-right: 4px; /* make room for the exclamation icon */
}
/* Fix math alignment, see https://github.com/rtfd/sphinx_rtd_theme/pull/686 */
.math {
text-align: unset;
}
</style>
<div class="section" id="安装Python">
<h1>安装Python<a class="headerlink" href="#安装Python" title="永久链接至标题">¶</a></h1>
<p>废话不多说,让我们提供安装Python和其它有用的Python模组的步骤吧。安装Anaconda平台会安装以下的内容:</p>
<ul class="simple">
<li><p>Python;具体来讲是之前一节提到的CPython直译器。</p></li>
<li><p>一些有用的Python模组,如matplotlib,NumPy,和SciPy。</p></li>
<li><p>Jupyter,用以提供用来编写新代码的互动式“记事本”环境。</p></li>
<li><p>conda:一个帮助你安装并升级Python和其它Python模组以及自动处理兼容性问题的包管理器。</p></li>
</ul>
<p>请注意通过Anaconda安装Python<strong>不会</strong>破坏通过其他途径安装的Python。详阅<a class="reference external" href="#我刚对我的电脑做了什么?">我刚对我的电脑做了什么?</a>。</p>
<p>有些Anaconda提供的模组,如NumPy,是被<a class="reference external" href="https://docs.anaconda.com/mkl-optimizations/">优化</a>过的,而因此比起单独安装运行得快很多。</p>
<div class="admonition note">
<p class="admonition-title fa fa-exclamation-circle"><strong>经验</strong>:</p>
<p>“Anaconda”是包含CPython直译器和其它流行Python数据科学模组的集合。它提供一个用来下载和更新Python模组的包管理器,以及一个用以同时维持多个独立Python安装的环境管理器。</p>
</div>
<div class="section" id="安装Anaconda">
<h2>安装Anaconda<a class="headerlink" href="#安装Anaconda" title="永久链接至标题">¶</a></h2>
<ol class="arabic simple">
<li><p>点开<a class="reference external" href="https://www.anaconda.com/download/">本链接</a>,并单击<strong>Python 3</strong>对应的“Download”按钮。</p></li>
<li><p>当下载完成后,安装下载的文件。安装过程中会有个选择:<code class="docutils literal notranslate"><span class="pre">Add</span> <span class="pre">Anaconda</span> <span class="pre">to</span> <span class="pre">the</span> <span class="pre">system</span> <span class="pre">PATH</span> <span class="pre">environment</span> <span class="pre">variable</span></code>(将Anaconda加到系统PATH的环境变量中);我建议你<strong>选择</strong>这个选项(懂行的用户:见下以了解关于这个选择的注意事项)。</p></li>
<li><p>完成20分钟的<a class="reference external" href="https://conda.io/projects/conda/en/latest/user-guide/getting-started.html">“Getting Started”</a>(入门)教程来熟悉 <code class="docutils literal notranslate"><span class="pre">conda</span></code>。这很重要!译者注:这里是英文的文档,有能力的读者可以用谷歌机翻。如果实在看不懂,文章中关于环境管理的内容在本节后面会有包含。</p></li>
</ol>
<p>你将会需要知道如何打开一个命令行(Windows用户是cmd.exe),以及如何在命令行中切换文件夹。如果你不知道如何做到这些,请阅读一个和你操作系统相对应的教程。译者注:Windows:<a class="reference external" href="https://wenku.baidu.com/view/35c6365c7cd184254a353526.html">文库</a>(注意要下载券)或<a class="reference external" href="https://jingyan.baidu.com/article/adc815138b23eef722bf7348.html">经验</a>(不全面,但够用)。</p>
</div>
<div class="section" id="我刚对我的电脑做了什么?">
<h2>我刚对我的电脑做了什么?<a class="headerlink" href="#我刚对我的电脑做了什么?" title="永久链接至标题">¶</a></h2>
<p>这创建了一个叫做 <code class="docutils literal notranslate"><span class="pre">Anaconda3</span></code>(或类似)的文件夹。它包含了所以和CPython直译器有关的文件,所有Python标准库的模组,之前提到过的Anaconda包含的第三方模组(例,NumPy,SciPy,Jupyter,iPython),和 <code class="docutils literal notranslate"><span class="pre">conda</span></code> 包管理器。它也包含了这些所有的程序的可执行文件。默认的Anaconda安装路径为:</p>
<ul class="simple">
<li><p>(Linux):<code class="docutils literal notranslate"><span class="pre">/home/<your_username>/Anaconda3</span></code></p></li>
<li><p>(Windows):<code class="docutils literal notranslate"><span class="pre">C:\Users\<your_username>\Anaconda3</span></code></p></li>
<li><p>(Mac):<code class="docutils literal notranslate"><span class="pre">/Users/<your_username>/Anaconda3</span></code></p></li>
</ul>
<p>如果你跟随了上文的安装指示,那么Anaconda安装器应该<em>将这个文件夹输入到了你系统的“path”环境变量中</em>。让我们快速地描述一下这具体意味着什么。系统的path是一个文件夹列表。每当你从你计算机的命令行中执行任何指令,你的计算机都会快速搜寻“path”包含的文件夹下面同名的可执行文件;<em>计算机将会执行它找到的第一个同名可执行文件</em>。因此,将 <code class="docutils literal notranslate"><span class="pre">Anaconda3</span></code> 文件夹放在path列表的最顶端会导致你的系统最优先使用Anaconda的Python可执行文件,而非任何其它安装的Python版本,因为系统会首先找到Anaconda版本的可执行文件。</p>
<p>对Linux和Mac用户而说,你的系统很有可能已经安装了某个版本的Python。<em>千万不要尝试卸载,删除,或修改系统自带的Python</em>。这些操作系统用它们自带的Python去执行系统的一些服务。这些服务会直接专门调用系统自带的Python可执行文件——它们不会意外地使用Anaconda版本的Python。所以你要知道的就是你可以安装Anaconda而不担心以上这些细节。</p>
<p><strong>对用其它编程语言写代码的人的重要事项:</strong>Anaconda使用它自己的 <code class="docutils literal notranslate"><span class="pre">lib</span></code> 和 <code class="docutils literal notranslate"><span class="pre">bin</span></code> 文件夹来存储模组文件和二进制文件。虽然这让用户可以简单地安装复杂的使用C库的Python模组,但是这也意味着你的系统会首先使用Anaconda的文件而不是你的系统的 <code class="docutils literal notranslate"><span class="pre">lib</span></code> 和 <code class="docutils literal notranslate"><span class="pre">bin</span></code>。如果你编写其它语言的代码,这可能会是个大问题。</p>
<p>最简单的解决方案就是<em>不要</em>让Anaconda安装器将Anaconda包含在你的path里面。你可以创建一行alias(代名)来手动前置Anaconda到你的path里。比如说,在Linux上你可以在 <code class="docutils literal notranslate"><span class="pre">~/.bashrc</span></code> 文件中添加如下的alias:</p>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span><span class="nb">alias</span> <span class="nv">anaconda</span><span class="o">=</span><span class="s2">"export PATH=/home/<your_username>/anaconda3/bin:</span><span class="nv">$PATH</span><span class="s2">"</span>
</pre></div>
</div>
<p>设置好这个alias后,你可以从命令台调用 <code class="docutils literal notranslate"><span class="pre">anaconda</span></code> 来将Anaconda暂时添加到path的顶端——这只维持到你当前命令台被关闭之前。</p>
</div>
<div class="section" id="快速入门Conda环境">
<h2>快速入门Conda环境<a class="headerlink" href="#快速入门Conda环境" title="永久链接至标题">¶</a></h2>
<p><code class="docutils literal notranslate"><span class="pre">conda</span></code> 不仅仅是一个包管理器,它也同时是一个很强大的环境管理器。在了解它之前,让我们先看看为什么我们会需要一个环境管理器:</p>
<blockquote>
<div><p>PLYMI建议你使用最新版本的Python(Python 3.X)来阅读本资源。但是,很多时候其它的课程或项目会需要你使用Python 2.7,一个已经不被更新或维护的Python版本。有没有一个简单干净的方法来快速在Python 3和Python2.7环境中切换呢?有!使用conda环境可以完美解决这个问题。</p>
</div></blockquote>
<p>假设你的Anaconda安装包含了Python 3,那你的<em>root</em>(默认)conda环境就是这个Python 3环境。你现在可以创建一个包含Python 2.7和所有Anaconda自带的第三方模组的conda环境。在你的命令行中执行以下指令:</p>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>conda create -n py27 <span class="nv">python</span><span class="o">=</span><span class="m">2</span>.7 anaconda
</pre></div>
</div>
<p>当安装成功后,你将可以激活(activate)这个叫做 <code class="docutils literal notranslate"><span class="pre">py27</span></code> 的环境。在命令行中执行以下命令:</p>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>conda activate py27
</pre></div>
</div>
<p>激活一个环境仅仅会更新你的系统path变量。在本例中这意味着 <code class="docutils literal notranslate"><span class="pre">Anaconda3</span></code> 会被更新为 <code class="docutils literal notranslate"><span class="pre">Anacond3/envs/py27</span></code>。这样你的系统就会在path中找到Python 2.7的可执行文件还有该版本相对应的模组。请注意这个变化仅仅作用于<strong>当前的命令行进程</strong>。任何其它的命令行进程都会默认使用叫做root的conda环境。</p>
<p>当你激活名为 <code class="docutils literal notranslate"><span class="pre">py27</span></code> 的环境后,你可以使用Python命令行,iPython命令行,Jupyter记事本,执行Python脚本,等等。这些现在都会使用Python 2.7。你可以用 <code class="docutils literal notranslate"><span class="pre">conda</span></code>(和 <code class="docutils literal notranslate"><span class="pre">pip</span></code>)来在这个环境中安装和Python2.7兼容的模组。只要你的path指向的是 <code class="docutils literal notranslate"><span class="pre">Anaconda3/envs/py27</span></code> 而不是 <code class="docutils literal notranslate"><span class="pre">Anaconda3</span></code>,这些指令的行为就会如同Python 2.7是计算机上唯一的Python版本一般。</p>
<p>冻结(<code class="docutils literal notranslate"><span class="pre">deactivate</span></code>)这个环境将将你返回到名为root的Python 3环境。这意味着conda将重新修改path,让它包含 <code class="docutils literal notranslate"><span class="pre">Anaconda3</span></code> 而不是 <code class="docutils literal notranslate"><span class="pre">Anaconda3/envs/py27</span></code>。执行以下指令:</p>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>conda deactivate
</pre></div>
</div>
<p>就这样,conda环境提供给你像招魂师一样的能力,轻便地在活的(Python 3)和死的(Python 2.7)之间切换。</p>
<p>除了在Python 3和2之间切换,conda环境有着很多其它的作用。很多人喜欢为每一个新的大规模项目创建一个新的conda环境。这样他们可以随意安装任何该项目需要的模组而不用担心和其他项目模组的冲突。读者应当注意学习和使用conda环境。</p>
<p>我非常建议你去阅读<a class="reference external" href="https://conda.io/docs/user-guide/tasks/manage-environments.html">这个管理conda环境的教程</a>。译者注:同样是英文的文档,可以考虑不看。</p>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="Informal_Intro_Python.html" class="btn btn-neutral float-right" title="非正式的Python介绍" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="GettingStartedWithPython.html" class="btn btn-neutral float-left" title="介绍Python编程语言" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
© Copyright 2019, Ryan Soklaski
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/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">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>