You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am developing a package which can be imported or can be executed from command line as an module. I am currently wiring all the packages inside the __init__.py file. So when I import the package the __init__.py file helps to initialize the resources in my container and wires it -
from .containers import Container
container = Container()
container.init_resources()
container.wire(packages=[__name__])
And when my package is ran like an module then the control should go to __main__.py file and helps with initializing container resources and wiring the packages.
I noticed when I added __main__.py file and imported my package, the control will go to __init__.py to initialize the resources and post wiring control it again goes to the __main__.py file. This is causing the container to be initialized twice and wires the packages.
Also vice-versa, when I run my package as an module the control is going to __init__.py and later to the __main__.py. Is this expected?
Expected behavior should be -
If imported -> container should be initialized and packages should be wired inside the __init__.py file.
If ran as an module -> container should be initialized and packages should be wired inside the __main__.py file.
EDIT:
So I figured if I run the package with -m then it will run the __init__.py file. But still I am not able to figure out when I import my package and run it, why it is still running the __main__.py? I am not importing __main__.py anywhere.
EDIT 2:
I figured it is container.wire(packages=[__name__]) causing to run __main__.py content.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am developing a package which can be imported or can be executed from command line as an module. I am currently wiring all the packages inside the
__init__.py
file. So when I import the package the__init__.py
file helps to initialize the resources in my container and wires it -And when my package is ran like an module then the control should go to
__main__.py
file and helps with initializing container resources and wiring the packages.I noticed when I added
__main__.py
file and imported my package, the control will go to__init__.py
to initialize the resources and post wiring control it again goes to the__main__.py
file. This is causing the container to be initialized twice and wires the packages.Also vice-versa, when I run my package as an module the control is going to
__init__.py
and later to the__main__.py
. Is this expected?Expected behavior should be -
__init__.py
file.__main__.py
file.EDIT:
So I figured if I run the package with
-m
then it will run the__init__.py
file. But still I am not able to figure out when I import my package and run it, why it is still running the__main__.py
? I am not importing__main__.py
anywhere.EDIT 2:
I figured it is
container.wire(packages=[__name__])
causing to run__main__.py
content.Beta Was this translation helpful? Give feedback.
All reactions