Skip to content

Commit ac13528

Browse files
CooCoo
Coo
authored and
Coo
committed
10.9小节完成
1 parent cc4e51f commit ac13528

File tree

1 file changed

+11
-32
lines changed

1 file changed

+11
-32
lines changed

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)