Importing variables from config.py files - what is happening under the hood?

Viewed 25

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:

  1. mainthing.py

    • from test import config
      • because we're saying from test, the __init__.py file in the test package runs first.
  2. __init__.py

    • from test import config
      • This imports config.py into the namespace of __init__.py
    • `config.username = "username-init"
      • inside the namespace of __init__.py, we give the config.username variable a value of "username-init"
  3. mainthing.py

    • still on the from test import config line, we have just ran __init__.py and now we can proceed with importing the config.py module from the test package into the namespace of mainthing.py

    • print(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.py imports 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 a config.py file, when I use from test in my mainthing.py file and then __init__.py implicitly runs as part of that line of code, I'm then importing into mainthing.py the namespace of __init__.py which would itself contain its own config.username variable inside its own __init__.py namespace. Assuming this is correct, what then happens in mainthing.py when Python then encounters the import config part of the line from test import config, seeing that the from test part in mainthing.py has already run? Would it see that config was already implicitly loaded in the namespace of __init__.py in the first part of the from...import command (from test) and that it doesn't need to be loaded into mainthing.py again ? If so, mainthing.py would get the config.username value that was in the namespace of __init__.py ("username-init") and not in the namespace of config.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!
0 Answers
Related