简体   繁体   English

包__init__.py文件中定义的导入范围是什么?

[英]What is the scope of imports defined in the package __init__.py file?

For a project I had to switch from C++ to python but I'm having some trouble fully understanding the __init__.py file. 对于一个项目,我不得不从C ++切换到python,但是在完全理解__init__.py文件时遇到了一些麻烦。 I found the following documentation on the __init__.py file: 我在__init__.py文件中找到了以下文档:

To get more familiar with the way the __init__.py file works I did some tests in both python 2.7 and 3.6.5. 为了更熟悉__init__.py文件的工作方式,我在python 2.7和3.6.5中都进行了一些测试。 I used the "PythonCentral" test pacakge (see link 2) for this: 我为此使用了“ PythonCentral”测试包(请参见链接2):

包装概述

Now I understand the following about the __init__.py file: 现在,我了解了有关__init__.py文件的以下内容:

  • The __init__.py file executes when a module or subpackage module is imported. 导入模块或子包模块时,将执行__init__.py文件。
  • The __init__.py can be used to overload the package __all__ method. __init__.py可用于重载__all__方法包。
  • The __init__.py can be used to define the import order __init__.py可用于定义导入顺序
  • The __init__.py can be used to make classes available on package and subpackage level. __init__.py可用于使类在包和子包级别可用。

Using the different modules and classes from the scope of my main file seems to go as explained in the documentation. 如文档中所述,似乎使用了主文件范围内的不同模块和类。 However when trying to use a class that is defined in another subpackage module I run into troubles. 但是,当尝试使用另一个子包模块中定义的类时,我遇到了麻烦。 In python 2.7 created the following __init__.py file in the subanimals subpackage: 在python 2.7中,在subanimals子包中创建了以下__init__.py文件:

from Mammals import Mammals

print "subpackage __init__.py executed"

Following I created the following code inside the Bird.py module: 接下来,我在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

When running the main following main script: 运行以下主要主要脚本时:

    from animals.subanimals import Birds

test = Birds()
test.printMembers()

I keep getting the global name Mammals not defined. 我不断得到未定义的哺乳动物的全局名称。 I can solve this in python 2.7 by adding from Mammals import Mammals to the top of the Birds.py. 我可以在python 2.7中解决此问题,方法是从“哺乳动物”中将“导入哺乳动物”添加到Birds.py的顶部。

Does the __init__.py only import packages and classes on the scope level of the main script and therefore not inside the Birds.py module or am I doing something else wrong? __init__.py是否仅在主脚本的作用域级别上导入包和类,因此不导入Birds.py模块内部,还是我做错了其他事情?

Answer to my question can be found in @jonrsharpe 's comment. 我的问题的答案可以在@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; init .py用于将目录中的多个文件聚合到一个模块中; think of it as grouping and passing attributes up to the parent, not down to the other files in that directory. 可以将其视为将属性分组并传递给父级,而不是传递给该目录中的其他文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM