-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvenv.html
More file actions
307 lines (286 loc) · 21.4 KB
/
venv.html
File metadata and controls
307 lines (286 loc) · 21.4 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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_TW">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>12. 虛擬環境與套件 — Python 3.7.0 說明文件</title>
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></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="../_static/translations.js"></script>
<script type="text/javascript" src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.7.0 說明文件 中搜尋"
href="../_static/opensearch.xml"/>
<link rel="author" title="關於這些文件" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜尋" href="../search.html" />
<link rel="copyright" title="Copyright" href="../copyright.html" />
<link rel="next" title="13. 現在可以來學習些什麼?" href="whatnow.html" />
<link rel="prev" title="11. Brief Tour of the Standard Library — Part II" href="stdlib2.html" />
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
<link rel="canonical" href="https://docs.python.org/3/tutorial/venv.html" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/switchers.js"></script>
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>瀏覽</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模組索引"
>模組</a> |</li>
<li class="right" >
<a href="whatnow.html" title="13. 現在可以來學習些什麼?"
accesskey="N">下一頁</a> |</li>
<li class="right" >
<a href="stdlib2.html" title="11. Brief Tour of the Standard Library — Part II"
accesskey="P">上一頁</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<span class="language_switcher_placeholder">zh_TW</span>
<span class="version_switcher_placeholder">3.7.0</span>
<a href="../index.html">Documentation </a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python 教學</a> »</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="virtual-environments-and-packages">
<span id="tut-venv"></span><h1>12. 虛擬環境與套件<a class="headerlink" href="#virtual-environments-and-packages" title="本標題的永久連結">¶</a></h1>
<div class="section" id="introduction">
<h2>12.1. 簡介<a class="headerlink" href="#introduction" title="本標題的永久連結">¶</a></h2>
<p>Python 應用程式通常會用到不在標準函式庫的套件和模組。應用程式有時候會需要某個特定版本的函式庫,因為這個應用程式可能需要某個特殊的臭蟲修正,或是這個應用程式是根據該函式庫特定版本的介面所撰寫。</p>
<p>這意味著不太可能安裝一套 Python 就可以滿足所有應用程式的要求。如果應用程式 A 需要一個特定的模組的 1.0 版,但另外一個應用程式 B 需要 2.0 版,那麼這整個需求不管安裝 1.0 或是 2.0 都會衝突,以致於應用程式無法使用。</p>
<p>解決方案是創建一個<a class="reference internal" href="../glossary.html#term-virtual-environment"><span class="xref std std-term">虛擬環境(virtual environment)</span></a>,這是一個獨立的資料夾,並且裡面裝好了特定版本的 Python,以及一系列相關的套件。</p>
<p>不同的應用程式可以使用不同的虛擬環境。以前述中需要被解決的例子中,應用程式 A 能夠擁有它自己的虛擬環境,並且是裝好 1.0 版,然而應用程式 B 則可以是用另外一個有 2.0 版的虛擬環境。要是應用程式 B 需要某個函式庫被升級到 3.0 版,這並不會影響到應用程式 A 的環境。</p>
</div>
<div class="section" id="creating-virtual-environments">
<h2>12.2. 建立虛擬環境<a class="headerlink" href="#creating-virtual-environments" title="本標題的永久連結">¶</a></h2>
<p>用來建立與管理虛擬環境的模組叫做 <a class="reference internal" href="../library/venv.html#module-venv" title="venv: Creation of virtual environments."><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code></a>。<a class="reference internal" href="../library/venv.html#module-venv" title="venv: Creation of virtual environments."><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code></a> 通常會安裝你能夠取得的最新版本的 Python。要是你的系統有不同版本的 Python,你可以透過 <code class="docutils literal notranslate"><span class="pre">python3</span></code> 這個指令選擇特定或是任意版本的 Python。</p>
<p>在建立虛擬環境的時候,在你決定要放該虛擬環境的資料夾之後,在 script 中執行 <a class="reference internal" href="../library/venv.html#module-venv" title="venv: Creation of virtual environments."><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code></a> 模組並且給定資料夾 path:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python3</span> <span class="o">-</span><span class="n">m</span> <span class="n">venv</span> <span class="n">tutorial</span><span class="o">-</span><span class="n">env</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">tutorial-env</span></code> 不存在的話,這會建立 <code class="docutils literal notranslate"><span class="pre">tutorial-env</span></code> 資料夾,並且也會在裡面建立一個有 Python 直譯器的複本、標準函式庫、以及不同的支援檔案的資料夾。</p>
<p>一旦你建立了一個虛擬環境,你可以啟動他。</p>
<p>在 Windows 系統中,使用:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">tutorial</span><span class="o">-</span><span class="n">env</span>\<span class="n">Scripts</span>\<span class="n">activate</span><span class="o">.</span><span class="n">bat</span>
</pre></div>
</div>
<p>在 Unix 或 MacOS 系統,使用:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">source</span> <span class="n">tutorial</span><span class="o">-</span><span class="n">env</span><span class="o">/</span><span class="nb">bin</span><span class="o">/</span><span class="n">activate</span>
</pre></div>
</div>
<p>(這段程式碼適用於 bash shell。如果你是用 <strong class="program">csh</strong> 或者 <strong class="program">fish</strong> shell,應當使用替代的 <code class="docutils literal notranslate"><span class="pre">activate.csh</span></code> 與 <code class="docutils literal notranslate"><span class="pre">activate.fish</span></code> 腳本。)</p>
<p>啟動虛擬環境會改變你的 shell 提示字元來顯示你正在使用的虛擬環境,並且修改環境以讓你在執行 <code class="docutils literal notranslate"><span class="pre">python</span></code> 的時候可以得到特定的 Python 版本,例如說:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$ <span class="nb">source</span> ~/envs/tutorial-env/bin/activate
<span class="o">(</span>tutorial-env<span class="o">)</span> $ python
Python <span class="m">3</span>.5.1 <span class="o">(</span>default, May <span class="m">6</span> <span class="m">2016</span>, <span class="m">10</span>:59:36<span class="o">)</span>
...
>>> import sys
>>> sys.path
<span class="o">[</span><span class="s1">''</span>, <span class="s1">'/usr/local/lib/python35.zip'</span>, ...,
<span class="s1">'~/envs/tutorial-env/lib/python3.5/site-packages'</span><span class="o">]</span>
>>>
</pre></div>
</div>
</div>
<div class="section" id="managing-packages-with-pip">
<h2>12.3. 用 pip 管理套件<a class="headerlink" href="#managing-packages-with-pip" title="本標題的永久連結">¶</a></h2>
<p>你可以使用一個叫做 <strong class="program">pip</strong> 的程式來安裝、升級和移除套件。<code class="docutils literal notranslate"><span class="pre">pip</span></code> 預設會從 Python Package Index <<a class="reference external" href="https://pypi.org">https://pypi.org</a>> 安裝套件。你可以透過你的瀏覽器瀏覽 Python Package Index,或是使用 <code class="docutils literal notranslate"><span class="pre">pip</span></code> 的限定搜索功能:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span> $ pip search astronomy
skyfield - Elegant astronomy <span class="k">for</span> Python
gary - Galactic astronomy and gravitational dynamics.
novas - The United States Naval Observatory NOVAS astronomy library
astroobs - Provides astronomy ephemeris to plan telescope observations
PyAstronomy - A collection of astronomy related tools <span class="k">for</span> Python.
...
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">pip</span></code> 有好幾個子指令:」search」、」install」、」uninstall」、」freeze」 等等。(這可以參考<a class="reference internal" href="../installing/index.html#installing-index"><span class="std std-ref">安裝 Python 模組</span></a>說明書來取得 <code class="docutils literal notranslate"><span class="pre">pip</span></code> 的完整文件說明。)</p>
<p>你可以透過指定套件名字來安裝最新版本的套件:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span> $ pip install novas
Collecting novas
Downloading novas-3.1.1.3.tar.gz <span class="o">(</span>136kB<span class="o">)</span>
Installing collected packages: novas
Running setup.py install <span class="k">for</span> novas
Successfully installed novas-3.1.1.3
</pre></div>
</div>
<p>你也可以透過在套件名稱之後接上 <code class="docutils literal notranslate"><span class="pre">==</span></code> 和版號來指定特定版本:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span> $ pip install <span class="nv">requests</span><span class="o">==</span><span class="m">2</span>.6.0
Collecting <span class="nv">requests</span><span class="o">==</span><span class="m">2</span>.6.0
Using cached requests-2.6.0-py2.py3-none-any.whl
Installing collected packages: requests
Successfully installed requests-2.6.0
</pre></div>
</div>
<p>要是你重新執行此指令,<code class="docutils literal notranslate"><span class="pre">pip</span></code> 會知道該版本已經安裝過,然後什麼也不做。你可以提供不同的版本號碼來取得該版本,或是可以執行 <code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">--upgrade</span></code> 來把套件升級到最新的版本:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span> $ pip install --upgrade requests
Collecting requests
Installing collected packages: requests
Found existing installation: requests <span class="m">2</span>.6.0
Uninstalling requests-2.6.0:
Successfully uninstalled requests-2.6.0
Successfully installed requests-2.7.0
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">uninstall</span></code> 後面接一個或是多個套件名稱可以從虛擬環境中移除套件。</p>
<p><code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">show</span></code> 可以顯示一個特定套件的資訊:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span> $ pip show requests
---
Metadata-Version: <span class="m">2</span>.0
Name: requests
Version: <span class="m">2</span>.7.0
Summary: Python HTTP <span class="k">for</span> Humans.
Home-page: http://python-requests.org
Author: Kenneth Reitz
Author-email: me@kennethreitz.com
License: Apache <span class="m">2</span>.0
Location: /Users/akuchling/envs/tutorial-env/lib/python3.4/site-packages
Requires:
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">list</span></code> 會顯示虛擬環境中所有已經安裝的套件:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span> $ pip list
novas <span class="o">(</span><span class="m">3</span>.1.1.3<span class="o">)</span>
numpy <span class="o">(</span><span class="m">1</span>.9.2<span class="o">)</span>
pip <span class="o">(</span><span class="m">7</span>.0.3<span class="o">)</span>
requests <span class="o">(</span><span class="m">2</span>.7.0<span class="o">)</span>
setuptools <span class="o">(</span><span class="m">16</span>.0<span class="o">)</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">freeze</span></code> 可以複製一整個已經安裝的套件清單,但是輸出使用 <code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span></code> 可以讀懂的格式。一個常見的慣例是放這整個清單到一個叫做 <code class="docutils literal notranslate"><span class="pre">requirements.txt</span></code> 的檔案:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span> $ pip freeze > requirements.txt
<span class="o">(</span>tutorial-env<span class="o">)</span> $ cat requirements.txt
<span class="nv">novas</span><span class="o">==</span><span class="m">3</span>.1.1.3
<span class="nv">numpy</span><span class="o">==</span><span class="m">1</span>.9.2
<span class="nv">requests</span><span class="o">==</span><span class="m">2</span>.7.0
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">requirements.txt</span></code> 可以提交到版本控制,並且作為釋出應用程式的一部分。使用者可以透過 <code class="docutils literal notranslate"><span class="pre">install</span> <span class="pre">-r</span></code> 安裝對應的的套件:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span> $ pip install -r requirements.txt
Collecting <span class="nv">novas</span><span class="o">==</span><span class="m">3</span>.1.1.3 <span class="o">(</span>from -r requirements.txt <span class="o">(</span>line <span class="m">1</span><span class="o">))</span>
...
Collecting <span class="nv">numpy</span><span class="o">==</span><span class="m">1</span>.9.2 <span class="o">(</span>from -r requirements.txt <span class="o">(</span>line <span class="m">2</span><span class="o">))</span>
...
Collecting <span class="nv">requests</span><span class="o">==</span><span class="m">2</span>.7.0 <span class="o">(</span>from -r requirements.txt <span class="o">(</span>line <span class="m">3</span><span class="o">))</span>
...
Installing collected packages: novas, numpy, requests
Running setup.py install <span class="k">for</span> novas
Successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">pip</span></code> 還有更多功能。可以參考<a class="reference internal" href="../installing/index.html#installing-index"><span class="std std-ref">安裝 Python 模組</span></a>說明書來取得完整的 <code class="docutils literal notranslate"><span class="pre">pip</span></code> 參考資料。當你撰寫了一個套件並且想要讓它可以在 Python Package Index 上可以取得的話,可以參考<a class="reference internal" href="../distributing/index.html#distributing-index"><span class="std std-ref">發布 Python 模組</span></a>說明。</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">目錄</a></h3>
<ul>
<li><a class="reference internal" href="#">12. 虛擬環境與套件</a><ul>
<li><a class="reference internal" href="#introduction">12.1. 簡介</a></li>
<li><a class="reference internal" href="#creating-virtual-environments">12.2. 建立虛擬環境</a></li>
<li><a class="reference internal" href="#managing-packages-with-pip">12.3. 用 pip 管理套件</a></li>
</ul>
</li>
</ul>
<h4>上個主題</h4>
<p class="topless"><a href="stdlib2.html"
title="上一章">11. Brief Tour of the Standard Library — Part II</a></p>
<h4>下個主題</h4>
<p class="topless"><a href="whatnow.html"
title="下一章">13. 現在可以來學習些什麼?</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">Report a Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.7/Doc/tutorial/venv.rst"
rel="nofollow">Show Source
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>瀏覽</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模組索引"
>模組</a> |</li>
<li class="right" >
<a href="whatnow.html" title="13. 現在可以來學習些什麼?"
>下一頁</a> |</li>
<li class="right" >
<a href="stdlib2.html" title="11. Brief Tour of the Standard Library — Part II"
>上一頁</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<span class="language_switcher_placeholder">zh_TW</span>
<span class="version_switcher_placeholder">3.7.0</span>
<a href="../index.html">Documentation </a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 教學</a> »</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="footer">
© <a href="../copyright.html">Copyright</a> 2001-2018, Python Software Foundation.
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
Last updated on 8月 22, 2018.
<a href="../bugs.html">Found a bug</a>?
<br />
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.7.7.
</div>
</body>
</html>