Skip to content

Commit 7cbba6e

Browse files
committed
增加“虚拟环境” 章节
1 parent 4a45eed commit 7cbba6e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1952
-622
lines changed

docs/html/.buildinfo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Sphinx build info version 1
22
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
3-
config: fc9deafe464ee1771927f377cd7283f3
3+
config: 140fb1222a934ddb93f559dc5229263a
44
tags: 645f666f9bcd5a90fca523b33c5a78b7

docs/html/_sources/controlflow.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ annotations)是定义在参数名称的冒号后面,紧随着一个用来表
561561
.. sectionauthor:: Georg Brandl <georg@python.org>
562562
.. index:: pair: coding; style
563563

564-
此时你已经可以写一此更长更复杂的 Python 程序,是时候讨论一下 *编码风格* 了。大多数语言可以写(或者更明白的说, *格式化* )作几种不同的风格。有些比其它的更好读。让你的代码对别人更易读是个好想法,养成良好的编码风格对此很有帮助。
564+
此时你已经可以写一些更长更复杂的 Python 程序,是时候讨论一下 *编码风格* 了。大多数语言可以写(或者更明白的说, *格式化* )作几种不同的风格。有些比其它的更好读。让你的代码对别人更易读是个好想法,养成良好的编码风格对此很有帮助。
565565

566566
对于 Python,`PEP 8`_ 引入了大多数项目遵循的风格指导。它给出了一个高度可读,视觉友好的编码风格。每个 Python 开发者都应该读一下,大多数要点都会对你有帮助:
567567

docs/html/_sources/index.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Python 是一门简单易学且功能强大的编程语言。它拥有高效的
3737
classes.rst
3838
stdlib.rst
3939
stdlib2.rst
40+
venv.rst
4041
whatnow.rst
4142
interactive.rst
4243
floatingpoint.rst

docs/html/_sources/venv.txt

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
2+
.. _tut-venv:
3+
4+
*********************************
5+
虚拟环境和包
6+
*********************************
7+
8+
简介
9+
============
10+
11+
Python 应用程序经常会使用一些不属于标准库的包和模块。应用程序有时候需要某个特定版本的库,因为它需要一个特定的 bug 已得到修复的库或者它是使用一个过时版本的库的接口编写的。
12+
13+
这就意味着可能无法安装一个 Python 来满足每个应用程序的要求。如果应用程序 A 需要一个特定模块的 1.0 版本但是应用程序 B 需要该模块的 2.0 版本,这两个应用程序的要求是冲突的,安装版本 1.0 或者版本 2.0 将会导致其中一个应用程序不能运行。
14+
15+
这个问题的解决方案就是创建一个 `虚拟环境 <https://docs.python.org/3/glossary.html#term-virtual-environment>`_ (通常简称为 “virtualenv”),包含一个特定版本的 Python,以及一些附加的包的独立的目录树。
16+
17+
接着不同的应用程序可以使用不同的虚拟环境。为了解决前面例子中的冲突,应用程序 A 可以有自己的虚拟环境,其中安装了特定模块的 1.0 版本。而应用程序 B 拥有另外一个安装了特定模块 2.0 版本的虚拟环境。如果应用程序 B 需求一个库升级到 3.0 的话,这也不会影响到应用程序 A 的环境。
18+
19+
20+
创建虚拟环境
21+
=============================
22+
23+
用于创建和管理虚拟环境的脚本叫做 :program:`pyvenv`。:program:`pyvenv` 通常会安装你可用的 Python 中最新的版本。这个脚本也能指定安装一个特定的版本的 Python,因此如果在你的系统中有多个版本的 Python 的话,你可以运行 ``pyvenv-3.4`` 或者你想要的任何版本来选择一个指定的 Python 版本。
24+
25+
要创建一个 virtualenv,首先决定一个你想要放置的目录接着运行 :program:`pyvenv` 后面携带着目录名::
26+
27+
pyvenv tutorial-env
28+
29+
如果目录不存在的话,这将会创建一个 ``tutorial-env`` 目录,并且也在目录里面创建一个包含 Python 解释器,标准库,以及各种配套文件的 Python “副本”。
30+
31+
一旦你已经创建了一个虚拟环境,你必须激活它。
32+
33+
在 Windows 上,运行::
34+
35+
tutorial-env/Scripts/activate
36+
37+
在 Unix 或者 MacOS 上,运行::
38+
39+
source tutorial-env/bin/activate
40+
41+
(这个脚本是用 bash shell 编写的。如果你使用 :program:`csh` 或者 :program:`fish` shell,你应该使用 ``activate.csh`` 和 ``activate.fish`` 来替代。)
42+
43+
激活了虚拟环境会改变你的 shell 提示符,显示你正在使用的虚拟环境,并且修改了环境以致运行 ``python`` 将会让你得到了特定的 Python 版本。例如::
44+
45+
-> source ~/envs/tutorial-env/bin/activate
46+
(tutorial-env) -> python
47+
Python 3.4.3+ (3.4:c7b9645a6f35+, May 22 2015, 09:31:25)
48+
...
49+
>>> import sys
50+
>>> sys.path
51+
['', '/usr/local/lib/python34.zip', ...,
52+
'~/envs/tutorial-env/lib/python3.4/site-packages']
53+
>>>
54+
55+
56+
使用 pip 管理包
57+
==========================
58+
59+
一旦你激活了一个虚拟环境,可以使用一个叫做 :program:`pip` 程序来安装,升级以及删除包。默认情况下 ``pip`` 将会从 Python Package Index,<https://pypi.python.org/pypi>, 中安装包。你可以通过 web 浏览器浏览它们,或者你也能使用 ``pip`` 有限的搜索功能::
60+
61+
(tutorial-env) -> pip search astronomy
62+
skyfield - Elegant astronomy for Python
63+
gary - Galactic astronomy and gravitational dynamics.
64+
novas - The United States Naval Observatory NOVAS astronomy library
65+
astroobs - Provides astronomy ephemeris to plan telescope observations
66+
PyAstronomy - A collection of astronomy related tools for Python.
67+
...
68+
69+
``pip`` 有许多子命令:“搜索”,“安装”,“卸载”,“freeze”(译者注:这个词语暂时没有合适的词语来翻译),等等。(请参考 `installing-index <https://docs.python.org/3/installing/index.html#installing-index>`_ 指南获取 ``pip`` 更多完整的文档。)
70+
71+
你可以安装一个包最新的版本,通过指定包的名称::
72+
73+
-> pip install novas
74+
Collecting novas
75+
Downloading novas-3.1.1.3.tar.gz (136kB)
76+
Installing collected packages: novas
77+
Running setup.py install for novas
78+
Successfully installed novas-3.1.1.3
79+
80+
你也能安装一个指定版本的包,通过给出包名后面紧跟着 ``==`` 和版本号::
81+
82+
-> pip install requests==2.6.0
83+
Collecting requests==2.6.0
84+
Using cached requests-2.6.0-py2.py3-none-any.whl
85+
Installing collected packages: requests
86+
Successfully installed requests-2.6.0
87+
88+
如果你重新运行命令(pip install requests==2.6.0),``pip`` 会注意到要求的版本已经安装不会再做其它事情。你也可以提供一个不同的版本号来安装,或者运行 ``pip
89+
install --upgrade`` 来升级包到最新版本::
90+
91+
-> pip install --upgrade requests
92+
Collecting requests
93+
Installing collected packages: requests
94+
Found existing installation: requests 2.6.0
95+
Uninstalling requests-2.6.0:
96+
Successfully uninstalled requests-2.6.0
97+
Successfully installed requests-2.7.0
98+
99+
``pip uninstall`` 后跟一个或者多个包名将会从虚拟环境中移除这些包。
100+
101+
``pip show`` 将会显示一个指定的包的信息::
102+
103+
(tutorial-env) -> pip show requests
104+
---
105+
Metadata-Version: 2.0
106+
Name: requests
107+
Version: 2.7.0
108+
Summary: Python HTTP for Humans.
109+
Home-page: http://python-requests.org
110+
Author: Kenneth Reitz
111+
Author-email: me@kennethreitz.com
112+
License: Apache 2.0
113+
Location: /Users/akuchling/envs/tutorial-env/lib/python3.4/site-packages
114+
Requires:
115+
116+
``pip list`` 将会列出所有安装在虚拟环境中的包::
117+
118+
(tutorial-env) -> pip list
119+
novas (3.1.1.3)
120+
numpy (1.9.2)
121+
pip (7.0.3)
122+
requests (2.7.0)
123+
setuptools (16.0)
124+
125+
``pip freeze`` 将会生成一个类似需要安装的包的列表,但是输出采用了 ``pip install`` 期望的格式。常见的做法就是把它们放在一个 ``requirements.txt`` 文件::
126+
127+
(tutorial-env) -> pip freeze > requirements.txt
128+
(tutorial-env) -> cat requirements.txt
129+
novas==3.1.1.3
130+
numpy==1.9.2
131+
requests==2.7.0
132+
133+
``requirements.txt`` 能够被提交到版本控制中并且作为一个应用程序的一部分。用户们可以使用 ``install -r`` 安装所有必须的包::
134+
135+
-> pip install -r requirements.txt
136+
Collecting novas==3.1.1.3 (from -r requirements.txt (line 1))
137+
...
138+
Collecting numpy==1.9.2 (from -r requirements.txt (line 2))
139+
...
140+
Collecting requests==2.7.0 (from -r requirements.txt (line 3))
141+
...
142+
Installing collected packages: novas, numpy, requests
143+
Running setup.py install for novas
144+
Successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0
145+
146+
``pip`` 还有更多的选项。请参考 `installing-index <https://docs.python.org/3/installing/index.html#installing-index>`_ 指南获取关于 ``pip`` 完整的文档。当你编写一个包并且在 Python Package Index 中也出现的话,请参考 `distributing-index <https://docs.python.org/3/distributing/index.html#distributing-index>`_ 指南。

docs/html/_static/basic.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* Sphinx stylesheet -- basic theme.
66
*
7-
* :copyright: Copyright 2007-2014 by the Sphinx team, see AUTHORS.
7+
* :copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
88
* :license: BSD, see LICENSE for details.
99
*
1010
*/
@@ -525,11 +525,11 @@ div.code-block-caption span.caption-number {
525525
div.code-block-caption span.caption-text {
526526
}
527527

528-
div.container {
528+
div.literal-block-wrapper {
529529
padding: 1em 1em 0;
530530
}
531531

532-
div.container div.highlight {
532+
div.literal-block-wrapper div.highlight {
533533
margin: 0;
534534
}
535535

docs/html/_static/doctools.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* Sphinx JavaScript utilities for all documentation.
66
*
7-
* :copyright: Copyright 2007-2014 by the Sphinx team, see AUTHORS.
7+
* :copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
88
* :license: BSD, see LICENSE for details.
99
*
1010
*/
@@ -179,7 +179,7 @@ var Documentation = {
179179
* see: https://bugzilla.mozilla.org/show_bug.cgi?id=645075
180180
*/
181181
fixFirefoxAnchorBug : function() {
182-
if (document.location.hash && $.browser.mozilla)
182+
if (document.location.hash)
183183
window.setTimeout(function() {
184184
document.location.href += '';
185185
}, 10);

docs/html/_static/searchtools.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
*
55
* Sphinx JavaScript utilties for the full-text search.
66
*
7-
* :copyright: Copyright 2007-2014 by the Sphinx team, see AUTHORS.
7+
* :copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
88
* :license: BSD, see LICENSE for details.
99
*
1010
*/
1111

1212

13+
/* Non-minified version JS is _stemmer.js if file is provided */
1314
/**
1415
* Porter Stemmer
1516
*/
@@ -373,8 +374,7 @@ var Search = {
373374
}
374375

375376
// lookup as search terms in fulltext
376-
results = results.concat(this.performTermsSearch(searchterms, excluded, terms, Scorer.term))
377-
.concat(this.performTermsSearch(searchterms, excluded, titleterms, Scorer.title));
377+
results = results.concat(this.performTermsSearch(searchterms, excluded, terms, titleterms));
378378

379379
// let the scorer override scores with a custom scoring function
380380
if (Scorer.score) {
@@ -439,7 +439,7 @@ var Search = {
439439
dataType: "text",
440440
complete: function(jqxhr, textstatus) {
441441
var data = jqxhr.responseText;
442-
if (data !== '') {
442+
if (data !== '' && data !== undefined) {
443443
listItem.append(Search.makeSearchSummary(data, searchterms, hlterms));
444444
}
445445
Search.output.append(listItem);
@@ -538,23 +538,47 @@ var Search = {
538538
/**
539539
* search for full-text terms in the index
540540
*/
541-
performTermsSearch : function(searchterms, excluded, terms, score) {
541+
performTermsSearch : function(searchterms, excluded, terms, titleterms) {
542542
var filenames = this._index.filenames;
543543
var titles = this._index.titles;
544544

545-
var i, j, file, files;
545+
var i, j, file;
546546
var fileMap = {};
547+
var scoreMap = {};
547548
var results = [];
548549

549550
// perform the search on the required terms
550551
for (i = 0; i < searchterms.length; i++) {
551552
var word = searchterms[i];
553+
var files = [];
554+
var _o = [
555+
{files: terms[word], score: Scorer.term},
556+
{files: titleterms[word], score: Scorer.title}
557+
];
558+
552559
// no match but word was a required one
553-
if ((files = terms[word]) === undefined)
560+
if ($u.every(_o, function(o){return o.files === undefined;})) {
554561
break;
555-
if (files.length === undefined) {
556-
files = [files];
557562
}
563+
// found search word in contents
564+
$u.each(_o, function(o) {
565+
var _files = o.files;
566+
if (_files === undefined)
567+
return
568+
569+
if (_files.length === undefined)
570+
_files = [_files];
571+
files = files.concat(_files);
572+
573+
// set score for the word in each file to Scorer.term
574+
for (j = 0; j < _files.length; j++) {
575+
file = _files[j];
576+
if (!(file in scoreMap))
577+
scoreMap[file] = {}
578+
scoreMap[file][word] = o.score;
579+
}
580+
});
581+
558582
// create the mapping
559583
for (j = 0; j < files.length; j++) {
560584
file = files[j];
@@ -576,14 +600,19 @@ var Search = {
576600
// ensure that none of the excluded terms is in the search result
577601
for (i = 0; i < excluded.length; i++) {
578602
if (terms[excluded[i]] == file ||
579-
$u.contains(terms[excluded[i]] || [], file)) {
603+
titleterms[excluded[i]] == file ||
604+
$u.contains(terms[excluded[i]] || [], file) ||
605+
$u.contains(titleterms[excluded[i]] || [], file)) {
580606
valid = false;
581607
break;
582608
}
583609
}
584610

585611
// if we have still a valid result we can add it to the result list
586612
if (valid) {
613+
// select one (max) score for the file.
614+
// for better ranking, we should calculate ranking by using words statistics like basic tf-idf...
615+
var score = $u.max($u.map(fileMap[file], function(w){return scoreMap[file][w]}));
587616
results.push([filenames[file], titles[file], '', null, score]);
588617
}
589618
}

docs/html/_static/websupport.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* sphinx.websupport utilties for all documentation.
66
*
7-
* :copyright: Copyright 2007-2014 by the Sphinx team, see AUTHORS.
7+
* :copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
88
* :license: BSD, see LICENSE for details.
99
*
1010
*/

0 commit comments

Comments
 (0)