Skip to content

Commit 6f07977

Browse files
committed
Merge pull request yidao620c#44 from MoguCloud/master
10.8小节完成
2 parents 1301d73 + ac13528 commit 6f07977

File tree

2 files changed

+21
-55
lines changed

2 files changed

+21
-55
lines changed

source/c10/p08_read_datafile_within_package.rst

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
----------
66
问题
77
----------
8-
Your package includes a datafile that your code needs to read. You need to do this in the
9-
most portable way possible.
8+
你的包中包含代码需要去读取的数据文件。你需要尽可能地用最便捷的方式来做这件事。
109

1110
|
1211
1312
----------
1413
解决方案
1514
----------
16-
Suppose you have a package with files organized as follows:
15+
假设你的包中的文件组织成如下:
1716

1817
.. code-block:: python
1918
@@ -22,45 +21,33 @@ Suppose you have a package with files organized as follows:
2221
somedata.dat
2322
spam.py
2423
25-
Now suppose the file spam.py wants to read the contents of the file somedata.dat. To do
26-
it, use the following code:
24+
现在假设spam.py文件需要读取somedata.dat文件中的内容。你可以用以下代码来完成:
2725

2826
.. code-block:: python
2927
3028
# spam.py
3129
import pkgutil
3230
data = pkgutil.get_data(__package__, 'somedata.dat')
3331
34-
The resulting variable data will be a byte string containing the raw contents of the file.
32+
由此产生的变量是包含该文件的原始内容的字节字符串。
3533

3634
|
3735
3836
----------
3937
讨论
4038
----------
41-
To read a datafile, you might be inclined to write code that uses built-in I/O functions,
42-
such as open(). However, there are several problems with this approach.
39+
要读取数据文件,你可能会倾向于编写使用内置的I/ O功能的代码,如open()。但是这种方法也有一些问题。
4340

4441

45-
First, a package has very little control over the current working directory of the interpreter.
46-
Thus, any I/O operations would have to be programmed to use absolute filenames.
47-
Since each module includes a __file__ variable with the full path, it’s not impossible
48-
to figure out the location, but it’s messy.
42+
首先,一个包对解释器的当前工作目录几乎没有控制权。因此,编程时任何I/O操作都必须使用绝对文件名。由于每个模块包含有完整路径的__file__变量,这弄清楚它的路径不是不可能,但它很凌乱。
4943

5044

51-
Second, packages are often installed as .zip or .egg files, which don’t preserve the files in
52-
the same way as a normal directory on the filesystem. Thus, if you tried to use open()
53-
on a datafile contained in an archive, it wouldn’t work at all.
45+
第二,包通常安装作为.zip或.egg文件,这些文件像文件系统上的一个普通目录一样不会被保留。因此,你试图用open()对一个包含数据文件的归档文件进行操作,它根本不会工作。
5446

5547

56-
The pkgutil.get_data() function is meant to be a high-level tool for getting a datafile
57-
regardless of where or how a package has been installed. It will simply “work” and return
58-
the file contents back to you as a byte string.
48+
pkgutil.get_data()函数是一个读取数据文件的高级工具,不用管包是如何安装以及安装在哪。它只是工作并将文件内容以字节字符串返回给你
5949

6050

61-
The first argument to get_data() is a string containing the package name. You can
62-
either supply it directly or use a special variable, such as __package__. The second
63-
argument is the relative name of the file within the package. If necessary, you can navigate
64-
into different directories using standard Unix filename conventions as long as the
65-
final directory is still located within the package.
51+
get_data()的第一个参数是包含包名的字符串。你可以直接使用包名,也可以使用特殊的变量,比如__package__。第二个参数是包内文件的相对名称。如果有必要,可以使用标准的Unix命名规范到不同的目录,只有最后的目录仍然位于包中。
52+
6653

source/c10/p09_add_directories_to_sys_path.rst

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55
----------
66
问题
77
----------
8-
You have Python code that can’t be imported because it’s not located in a directory listed
9-
in sys.path. You would like to add new directories to Python’s path, but don’t want to
10-
hardwire it into your code.
8+
你无法导入你的Python代码因为它所在的目录不在sys.path里。你想将添加新目录到Python路径,但是不想硬链接到你的代码。
9+
1110

1211
|
1312
1413
----------
1514
解决方案
1615
----------
17-
There are two common ways to get new directories added to sys.path. First, you can
18-
add them through the use of the PYTHONPATH environment variable. For example:
16+
有两种常用的方式将新目录添加到sys.path。第一种,你可以使用PYTHONPATH环境变量来添加。例如:
1917

2018
.. code-block:: python
2119
@@ -28,60 +26,41 @@ add them through the use of the PYTHONPATH environment variable. For example:
2826
['', '/some/dir', '/other/dir', ...]
2927
>>>
3028
31-
In a custom application, this environment variable could be set at program startup or
32-
through a shell script of some kind.
29+
在自定义应用程序中,这样的环境变量可在程序启动时设置或通过shell脚本。
3330
34-
The second approach is to create a .pth file that lists the directories like this:
31+
第二种方法是创建一个.pth文件,将目录列举出来,像这样:
3532
3633
.. code-block:: python
3734
3835
# myapplication.pth
3936
/some/dir
4037
/other/dir
4138
42-
This .pth file needs to be placed into one of Python’s site-packages directories, which are
43-
typically located at /usr/local/lib/python3.3/site-packages or ~/.local/lib/python3.3/sitepackages.
44-
On interpreter startup, the directories listed in the .pth file will be added to
45-
sys.path as long as they exist on the filesystem. Installation of a .pth file might require
46-
administrator access if it’s being added to the system-wide Python interpreter.
39+
这个.pth文件需要放在某个Python的site-packages目录,通常位于/usr/local/lib/python3.3/site-packages 或者 ~/.local/lib/python3.3/sitepackages。当解释器启动时,.pth文件里列举出来的存在于文件系统的目录将被添加到sys.path。安装一个.pth文件可能需要管理员权限,如果它被添加到系统级的Python解释器。
40+
4741
4842
|
4943
5044
----------
5145
讨论
5246
----------
53-
Faced with trouble locating files, you might be inclined to write code that manually
54-
adjusts the value of sys.path. For example:
47+
比起费力地找文件,你可能会倾向于写一个代码手动调节sys.path的值。例如:
5548
5649
.. code-block:: python
5750
5851
import sys
5952
sys.path.insert(0, '/some/dir')
6053
sys.path.insert(0, '/other/dir')
6154
62-
Although this “works,” it is extremely fragile in practice and should be avoided if possible.
63-
Part of the problem with this approach is that it adds hardcoded directory names
64-
to your source. This can cause maintenance problems if your code ever gets moved
65-
around to a new location. It’s usually much better to configure the path elsewhere in a
66-
manner that can be adjusted without making source code edits.
67-
68-
You can sometimes work around the problem of hardcoded directories if you carefully
69-
construct an appropriate absolute path using module-level variables, such as
70-
__file__. For example:
55+
虽然这能“工作”,它是在实践中极为脆弱,应尽量避免使用。这种方法的问题是,它将目录名硬编码到了你的源。如果你的代码被移到一个新的位置,这会导致维护问题。更好的做法是在不修改源代码的情况下,将path配置到其他地方。如果您使用模块级的变量来精心构造一个适当的绝对路径,有时你可以解决硬编码目录的问题,比如__file__。举个例子:
7156
7257
.. code-block:: python
7358
7459
import sys
7560
from os.path import abspath, join, dirname
7661
sys.path.insert(0, abspath(dirname('__file__'), 'src'))
7762
78-
This adds an src directory to the path where that directory is located in the same directory
79-
as the code that’s executing the insertion step.
63+
这将src目录添加到path里,和执行插入步骤的代码在同一个目录里。
8064
81-
The site-packages directories are the locations where third-party modules and packages
82-
normally get installed. If your code was installed in that manner, that’s where it would
83-
be placed. Although .pth files for configuring the path must appear in site-packages, they
84-
can refer to any directories on the system that you wish. Thus, you can elect to have
85-
your code in a completely different set of directories as long as those directories are
86-
included in a .pth file.
65+
site-packages目录是第三方包和模块安装的目录。如果你手动安装你的代码,它将被安装到site-packages目录。虽然.pth文件配置的path必须出现在site-packages里,但代码可以在系统上任何你想要的目录。因此,你可以把你的代码放在一系列不同的目录,只要那些目录包含在.pth文件里。
8766

0 commit comments

Comments
 (0)