I'm fairly new to Python and I'm trying to wrap my head around how a "config.py" module can help share variables across other modules in a Package. I've Googled for a while and have read several articles on python.org and elsewhere about various flavors of imports, and I've learned a lot about how imports work under the hood, and I think maybe I mostly have it but I still have a lingering question in terms of how from...import works when a __init__.py file is implicitly involved.
For example, say I have this simple package setup with simple modules (each module's simple code lines are put underneath each module for easier readability here):
test/
__init__.py
from test import config
config.username = "username-init"
config.py
username = "username-config"
mainthing.py
from test import config
print(config.username)
I can then open Python and run this:
import mainthing
'username-init' # it auto-prints "username-init" per the last line in mainthing.py
My question is around how mainthing.py gets the value of of the config.username variable that was edited by the __init__.py module. In my mind, here is the flow of operations when I run import mainthing in Python:
mainthing.pyfrom test import config- because we're saying
from test, the__init__.pyfile in thetestpackage runs first.
- because we're saying
__init__.pyfrom test import config- This imports
config.pyinto the namespace of__init__.py
- This imports
- `config.username = "username-init"
- inside the namespace of
__init__.py, we give theconfig.usernamevariable a value of "username-init"
- inside the namespace of
mainthing.pystill on the
from test import configline, we have just ran__init__.pyand now we can proceed with importing theconfig.pymodule from thetestpackage into the namespace ofmainthing.pyprint(config.username)- This prints "username-init", so the value that was set in the namespace of
__init__.py.- I guess this is where my question lies (apologies for the confusing novel that follows). When
mainthing.pyimports any module, it of course imports variables and functions from that module being imported (accessibility depends on how it was imported of course). But in the case of using aconfig.pyfile, when I usefrom testin mymainthing.pyfile and then__init__.pyimplicitly runs as part of that line of code, I'm then importing intomainthing.pythe namespace of__init__.pywhich would itself contain its ownconfig.usernamevariable inside its own__init__.pynamespace. Assuming this is correct, what then happens inmainthing.pywhen Python then encounters theimport configpart of the linefrom test import config, seeing that thefrom testpart inmainthing.pyhas already run? Would it see thatconfigwas already implicitly loaded in the namespace of__init__.pyin the first part of thefrom...importcommand (from test) and that it doesn't need to be loaded intomainthing.pyagain ? If so,mainthing.pywould get theconfig.usernamevalue that was in the namespace of__init__.py("username-init") and not in the namespace ofconfig.py("username-config"). As you can see I'm a bit mixed up on how it actually works. Again, I've read a lot of official and non-official documentation but I'm still a bit fuzzy on what is actually happening under the hood. Any help is appreciated. Thanks!
- I guess this is where my question lies (apologies for the confusing novel that follows). When
- This prints "username-init", so the value that was set in the namespace of