__main__.py 和 __init__.py 文件的无效行为

huangapple go评论53阅读模式
英文:

Invalid behavior of __main__.py and __init__.py files

问题

I am currently working on a Python package. The package can be imported or it can also run from command line with -m switch. Inside the package I am building a dependency injection container using - python-dependency-injector.

Now, here is my understanding of the __init__.py and __main__.py files -

  1. If package is imported - the content of __init__.py is executed.
  2. If package is ran from command line with -m - the content of __main__.py is executed.

For me weirdly, in either cases content of both files is being executed. Either I import the package or run it from command line the control flows through __init__.py to __main__.py and so on. All I am doing inside these files is building the dependency injection container -

from .containers import Container
container = Container()
container.init_resources()
container.wire(packages=[__name__])

When I run the package from command line I also get a RunTimeWarning as -
RunTimeWarning: 'package.__main__.py' found in sys_modules after import of package 'package', but prior to execution of 'package.__main__'; this may result in unpredictable behavior

And to fix this I added del sys.modules['package.__main__'] in __init__.py.

Here is my folder structure -

src
  \package
     \__init__.py
     \__main__.py
     \readers
        \__init__.py
        \reader_module.py
     \writers
        \__init__.py
        \writer_module.py
     \utility
     \__init__.py
     \container_module.py
  test.py <-- imports the package
英文:

I am currently working on a Python package. The package can be imported or it can also run from command line with -m switch. Inside the package I am building a dependency injection container using - python-dependency-injector.

Now, here is my understanding of the __init__.py and __main__.py files -

  1. If package is imported - the content of __init__.py is executed.
  2. If package is ran from command line with -m - the content of __main__.py is executed.

For me weirdly, in either cases content of both files is being executed. Either I import the package or run it from command line the control flows through __init__.py to __main__.py and so on. All I am doing inside these files is building the dependency injection container -

from .containers import Container
container = Container()
container.init_resources()
container.wire(packages=[__name__])

When I run the package from command line I also get a RunTimeWarning as -
RunTimeWarning: &#39;package.__main__.py&#39; found in sys_modules after import of package &#39;package&#39;, but prior to execution of &#39;package.__main__&#39;; this may result in unpredictable behavior

And to fix this I added del sys.modules[&#39;package.__main__&#39;] in __init__.py.

Here is my folder structure -

src
  \package
     \__init__.py
     \__main__.py
     \readers
        \__init__.py
        \reader_module.py
     \writers
        \__init__.py
        \writer_module.py
     \utility
     \__init__.py
     \container_module.py
  test.py &lt;-- imports the package

答案1

得分: 0

我发现依赖注入器包在我导入我的包时会导致加载__main__.py文件。具体来说,当容器被连接时 - container.wire(packages=[__name__])

目前,我决定保持__main__.py为空。

英文:

I figured that the dependency-injector package is causing to load __main__.py file when I import my package. Specifically, when the container is wired - container.wire(packages=[__name__]).|

For now, I have decided to keep __main__.py empty.

huangapple
  • 本文由 发表于 2023年7月13日 13:50:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676288.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定