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?)开发者_如何学Python that if you need connection.py
you already imported contrib
.
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.
You need to import contrib
in connection
. Either use a relative import (..contrib
) or an absolute import.
精彩评论