简体   繁体   English

两次调用__init__.py代码及其在包导入中的意义

[英]__init__.py code called twice and its significance with package import

I have a simple python project fro learning with two files __init__.py and __main__.py When I executed python -m pkg_name it runs both __init__.py and __main__.py 我有一个学习两个文件__init__.py__main__.py的简单python项目,当我执行python -m pkg_name它同时运行__init__.py__main__.py

When I execute python -m pkg_name.__init__.py it invokes __init__.py twice. 当我执行python -m pkg_name.__init__.py它将调用__init__.py两次。

I want to know why __init__.py is called twice when i call __init__.py Is it like the static code in java where when we call the class all the data in static code is automatically triggered. 我想知道为什么__init__.py当我打电话叫两次__init__.py是不是象Java在那里,当我们调用类的所有数据在静态代码自动触发的静态代码。

What is the relevance of __init__.py in python and benefits of it getting executed when package is imported/loaded or called for processing. __init__.py在python中的相关性是什么,以及在导入/加载或调用程序包进行处理时执行它的好处。 Please help me understand the concepts better. 请帮助我更好地理解这些概念。

"""Run a sequence of programs, testing python code __main__ variable

Each program (except the first) receives standard output of the
previous program on its standard input, by default. There are several
alternate ways of passing data between programs.

"""

def _launch():
    print('Pipeline Launched')


if __name__ == '__main__':
    print('This module is running as the main module!')
    _launch()
> __init__.py

"""This is the __init__.py file of pipleline package

Used for testing the import statements.


"""

print(__name__)
print('This is the __init__.py file of pipleline package')
print('Exiting __init__ of pipeline package after all initialization')

The following command is used to execute a Python module or package: 以下命令用于执行Python模块或软件包:

python -m module

Where module is the name of the module/package without .py extension. 其中module不带 .py扩展名的模块/软件包的名称。

  • if the name matches a script, it is byte-compiled and executed, 如果名称与脚本匹配,则将其编译并执行字节,
  • if the name matches a directory with a __init__.py file and a __main__.py file, the directory is considered as being a Python package and is loaded first. 如果名称与具有__init__.py文件和__main__.py文件的目录匹配,则该目录被视为Python软件包,并且将首先加载。 Then the __main__.py script is executed. 然后执行__main__.py脚本。
  • if the name contains dots, eg: "mylib.mypkg.mymodule", Python browse each package and sub-package matching the dotted name and load it. 如果名称包含点,例如:“ mylib.mypkg.mymodule”,Python将浏览与点名匹配的每个包和子包并加载它。 Then it execute the last module (or last package which must contain a __main__.py file). 然后,它执行最后一个模块(或最后一个必须包含__main__.py文件的包)。

A (short) description is done in the official documentation: 29.4. (简短的)描述在官方文档中完成: 29.4。 main — Top-level script environment . main —顶级脚本环境

Your problem 你的问题

If you run this command: 如果运行此命令:

python -m pkg_name

It loads (and run) the __init__.py and __main__.py : this is the normal behavior. 它加载(并运行) __init__.py__main__.py :这是正常行为。

If you run this command: 如果运行此命令:

python -m pkg_name.__init__.py

It should fail if you leave the ".py" extension. 如果保留“ .py”扩展名,则该操作将失败。

If it runs, the command loads the pkg_name package first: it execute the __init__.py first. 如果运行,该命令将首先加载pkg_name软件包:它将首先执行__init__.py Then it runs it again. 然后它再次运行。

It is used to define a folder as a package, which contains required modules and resources. 它用于将文件夹定义为包,其中包含必需的模块和资源。

You can use is as an empty file or add docs about the package or setup initial conditions for the module. 您可以将is用作一个空文件,也可以添加有关该软件包的文档或为该模块设置初始条件。

Please checkout the python documentation . 请签出python文档

Also, as mentioned by Natecat , __init__.py gets executed whenever you load a package. 另外,如Natecat所述 ,每次加载软件包时,都会执行__init__.py That's why when you explicitly call __init__.py , it loads the package (1st load) then executes __init__.py (2nd load). 这就是为什么当您显式调用__init__.py ,它将加载程序包(第一次加载),然后执行__init__.py__init__.py (第二次加载)。

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

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