包 __init__.py 文件中定义的导入范围是什么?
What is the scope of imports defined in the package __init__.py file?
对于一个项目,我不得不从 C++ 切换到 python,但我在完全理解 __init__.py
文件时遇到了一些麻烦。我在 __init__.py
文件中找到了以下文档:
- https://docs.python.org/3/tutorial/modules.html
- https://www.pythoncentral.io/how-to-create-a-python-package/
- http://mikegrouchy.com/blog/2012/05/be-pythonic-__init__py.html
为了更加熟悉 __init__.py
文件的工作方式,我在 python 2.7 和 3.6.5 中做了一些测试。为此,我使用了 "PythonCentral" 测试包(参见 link 2):
现在我对 __init__.py
文件的理解如下:
- 导入模块或子包模块时执行
__init__.py
文件。
__init__.py
可用于重载包 __all__
方法。
__init__.py
可以用来定义导入顺序
__init__.py
可用于使 classes 在包和子包级别上可用。
使用我的主文件范围内的不同模块和 classes 似乎按照文档中的说明进行。但是,当尝试使用在另一个子包模块中定义的 class 时,我 运行 遇到了麻烦。在 python 2.7 中,在 subanimals 子包中创建了以下 __init__.py
文件:
from Mammals import Mammals
print "subpackage __init__.py executed"
接下来我在 Bird.py 模块中创建了以下代码:
class Birds:
def __init__(self):
''' Constructor for this class. '''
# Create some member animals
self.members = ['Sparrow', 'Robin', 'Duck']
def printMembers(self):
print('Printing members of the Birds class')
for member in self.members:
print('\t%s ' % member)
test = Mammals()
test.printMembers
当运行ning主脚本如下:
from animals.subanimals import Birds
test = Birds()
test.printMembers()
我一直收到未定义的全局名称 Mammals。我可以在 python 2.7 中通过将 from Mammals import Mammals 添加到 Birds.py.
的顶部来解决这个问题
__init__.py
是否仅在主脚本的范围级别导入包和 classes,因此不在 Birds.py 模块内,或者我做错了什么?
我的问题的答案可以在@jonrsharpe 的评论中找到。
Each individual file must be valid on its own, defining all
appropriate imports. init.py is for aggregating multiple files in
a directory into a single module; think of it as grouping and passing
attributes up to the parent, not down to the other files in that
directory.
对于一个项目,我不得不从 C++ 切换到 python,但我在完全理解 __init__.py
文件时遇到了一些麻烦。我在 __init__.py
文件中找到了以下文档:
- https://docs.python.org/3/tutorial/modules.html
- https://www.pythoncentral.io/how-to-create-a-python-package/
- http://mikegrouchy.com/blog/2012/05/be-pythonic-__init__py.html
为了更加熟悉 __init__.py
文件的工作方式,我在 python 2.7 和 3.6.5 中做了一些测试。为此,我使用了 "PythonCentral" 测试包(参见 link 2):
现在我对 __init__.py
文件的理解如下:
- 导入模块或子包模块时执行
__init__.py
文件。 __init__.py
可用于重载包__all__
方法。__init__.py
可以用来定义导入顺序__init__.py
可用于使 classes 在包和子包级别上可用。
使用我的主文件范围内的不同模块和 classes 似乎按照文档中的说明进行。但是,当尝试使用在另一个子包模块中定义的 class 时,我 运行 遇到了麻烦。在 python 2.7 中,在 subanimals 子包中创建了以下 __init__.py
文件:
from Mammals import Mammals
print "subpackage __init__.py executed"
接下来我在 Bird.py 模块中创建了以下代码:
class Birds:
def __init__(self):
''' Constructor for this class. '''
# Create some member animals
self.members = ['Sparrow', 'Robin', 'Duck']
def printMembers(self):
print('Printing members of the Birds class')
for member in self.members:
print('\t%s ' % member)
test = Mammals()
test.printMembers
当运行ning主脚本如下:
from animals.subanimals import Birds
test = Birds()
test.printMembers()
我一直收到未定义的全局名称 Mammals。我可以在 python 2.7 中通过将 from Mammals import Mammals 添加到 Birds.py.
的顶部来解决这个问题__init__.py
是否仅在主脚本的范围级别导入包和 classes,因此不在 Birds.py 模块内,或者我做错了什么?
我的问题的答案可以在@jonrsharpe 的评论中找到。
Each individual file must be valid on its own, defining all appropriate imports. init.py is for aggregating multiple files in a directory into a single module; think of it as grouping and passing attributes up to the parent, not down to the other files in that directory.