英文:
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 -
- If package is imported - the content of
__init__.py
is executed. - 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 -
- If package is imported - the content of
__init__.py
is executed. - 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
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论