How to access __init__.py variables from deeper parts of a package

python

Solution

Adding `import contrib` to `connection.py` is the way to go. Yes, the `contrib` module is already imported (you can find out from `sys.modules`). The problem is there is no reference to the module from your code in connection.py. Doing another import will give you the reference. You do not need to worry about additional loading time because the module is already loaded.

Problem

I apologize for yet another `__init__.py` question. I have the following package structure: ``` +contrib +--__init__.py | +database +--__init__.py | +--connection.py ``` In the top-level `__init__.py` I define: `USER='me'`. If I `import contrib` from the command line, then I can access `contrib.USER`. Now, I want to access `contrib.user` from withih `connection.py` but I cannot do it. The top-level `__init__.py` is called when I issue `from contrib.database import connection`, so Python is really creating the parameter `USER`. So the question is: how to you access the parameters and variables declared in the top-level `__init__.py` from within the children. Thank you. EDIT: I realize that you can add `import contrib` to `connection.py`, but it seems repetitive, as it is obvious (incorrectly so?) that if you need `connection.py` you already imported `contrib`.

Original source