This is my app structure using python 3.5
app
__init__.py # running the code from here
module
__init__.py
test.py # want access to this
^-- test_function() is here
test2.py # and this one too
I am full aware I can access test with the following. note I am running this using the following cmd python 3 /app/__init__.py
need access to /app/module/test.py
from module import test
test.test_function()
# works
I can also import it into global scope (bad practice)
from module.test import test_function
# works
And I know i can also use
import module.test
# works
But what I would like to do is import the full module (or package sorry for my terminology)
I want to import the full package, example:
import module
module.test.test_function()
But I seem to get
AttributeError: module 'module' has no attribute 'test'
bonus question
If importing the full package is not a good practice then I don't mind being explicit and using from module import test
, please let me know.
PS I have tried adding imports in /app/module/__init__.py because it gets called during the import, but it seems that it doesn't work
I added import test
in /app/module/__init__.py
but when I try it test seems empty.
import module # module now has import test in its __init__
print(dir(module.test))
# ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
As you can see it's missing test_function()