55----------
66问题
77----------
8- You have code organized as a package and want to import a submodule from one of the
9- other package submodules without hardcoding the package name into the import
10- statement.
8+ 将代码组织成包,想用import语句从另一个包名没有硬编码过的包的中导入子模块。
9+
1110
12- |
1311
1412----------
1513解决方案
1614----------
17- To import modules of a package from other modules in the same package, use a packagerelative
18- import. For example, suppose you have a package mypackage organized as follows
19- on the filesystem:
15+ 使用包的相对导入,使一个的模块导入同一个包的另一个模块
16+ 举个例子,假设在你的文件系统上有mypackage包,组织如下:
17+
2018
2119.. code-block :: python
2220
@@ -30,32 +28,31 @@ on the filesystem:
3028 __init__ .py
3129 bar.py
3230
33- If the module mypackage .A.spam wants to import the module grok located in the same
34- directory, it should include an import statement like this:
31+ 如果模块mypackage .A.spam要导入同目录下的模块grok,它应该包括的import语句如下:
32+
3533
3634.. code-block :: python
3735
3836 # mypackage/A/spam.py
3937 from . import grok
4038
41- If the same module wants to import the module B. bar located in a different directory,
42- it can use an import statement like this:
39+ 如果模块mypackage.A.spam要导入不同目录下的模块B. bar,它应该使用的import语句如下:
40+
4341
4442.. code-block :: python
4543
4644 # mypackage/A/spam.py
4745 from ..B import bar
4846
49- Both of the import statements shown operate relative to the location of the spam.py file
50- and do not include the top-level package name.
47+ 两个import语句都没包含顶层包名,而是使用了spam.py的相对路径。
5148
5249|
5350
5451----------
5552讨论
5653----------
57- Inside packages, imports involving modules in the same package can either use fully
58- specified absolute names or a relative imports using the syntax shown. For example:
54+ 在包内,既可以使用相对路径也可以使用绝对路径来导入。
55+ 举个例子:
5956
6057.. code-block :: python
6158
@@ -64,47 +61,35 @@ specified absolute names or a relative imports using the syntax shown. For examp
6461 from . import grok # OK
6562 import grok # Error (not found)
6663
67- The downside of using an absolute name, such as mypackage.A, is that it hardcodes the
68- top-level package name into your source code. This, in turn, makes your code more
69- brittle and hard to work with if you ever want to reorganize it. For example, if you ever
70- changed the name of the package, you would have to go through all of your files and fix
71- the source code. Similarly, hardcoded names make it difficult for someone else to move
72- the code around. For example, perhaps someone wants to install two different versions
73- of a package, differentiating them only by name. If relative imports are used, it would
74- all work fine, whereas everything would break with absolute names.
64+ 像mypackage.A这样使用绝对路径名的不利之处是这将顶层包名硬编码到你的源码中。如果你想重新组织它,你的代码将更脆,很难工作。 举个例子,如果你改变了包名,你就必须检查所有文件来修正源码。 同样,硬编码的名称会使移动代码变得困难。举个例子,也许有人想安装两个不同版本的软件包,只通过名称区分它们。 如果使用相对导入,那一切都ok,然而使用绝对路径名很可能会出问题。
7565
7666
77- The ``. `` and ``.. `` syntax on the import statement might look funny, but think of it as specifying
78- a directory name. . means look in the current directory and ..B means look in
79- the ../B directory. This syntax only works with the from form of import. For example:
67+ import语句的 ``. `` 和 ``..``看起来很滑稽, 但它指定目录名.为当前目录,..B为目录../B。这种语法只适用于import。
68+ 举个例子:
8069
8170.. code-block:: python
8271
8372 from . import grok # OK
8473 import .grok # ERROR
8574
86- Although it looks like you could navigate the filesystem using a relative import, they are
87- not allowed to escape the directory in which a package is defined. That is, combinations
88- of dotted name patterns that would cause an import to occur from a non-package directory
89- cause an error.
75+ 尽管使用相对导入看起来像是浏览文件系统,但是不能到定义包的目录之外。也就是说,使用点的这种模式从不是包的目录中导入将会引发错误。
76+
9077
91- Finally, it should be noted that relative imports only work for modules that are located
92- inside a proper package. In particular, they do not work inside simple modules located
93- at the top level of scripts. They also won’t work if parts of a package are executed directly
94- as a script. For example:
78+ 最后,相对导入只适用于在合适的包中的模块。尤其是在顶层的脚本的简单模块中,它们将不起作用。如果包的部分被作为脚本直接执行,那它们将不起作用
79+ 例如:
9580
9681.. code-block:: python
9782
9883 % python3 mypackage/A/spam.py # Relative imports fail
9984
100- On the other hand, if you execute the preceding script using the -m option to Python,
101- the relative imports will work properly. For example:
85+ 另一方面,如果你使用Python的-m选项来执行先前的脚本,相对导入将会正确运行。
86+ 例如:
87+
10288
10389.. code-block:: python
10490
10591 % python3 -m mypackage.A.spam # Relative imports work
10692
107- For more background on relative package imports,
108- see `PEP 328 <http://www.python.org/dev/peps/pep-0328 >`_ .
93+ 更多的包的相对导入的背景知识,请看 `PEP 328 <http://www.python.org/dev/peps/pep-0328>`_ .
10994
11095
0 commit comments