Reputation: 886
I've been trying to figure out how to call a module after importing its parent package.
The directory structure looks like:
.
├── main
│ └── pkg
│ └── file.py
└── another_main
NOTE: All the packages contain __init__.py
file and all the necessary path variables are set. I've not shown it here as this is just a dummy structure.
If I do:
from main import pkg
pkg.file
This doesn't work and throws AttributeError: module 'pkg' has no attribute 'file'
But if first I do:
from main.pkg import file
After that I can do:
from main import pkg
pkg.file # --> Now this doesn't throw AttributeError
Is there a way I can call file
like pkg.file
without doing from main.pkg import file
?
P.S.: I want to use pkg.file
in my script so that in future I can recall that I called file
from pkg
and not some_other_pkg
.
Upvotes: 3
Views: 130
Reputation: 121
In your __init__.py
of main/pkg
you would have to import file
:
# pkg/__init__.py
import pkg.file
# EDIT: if the import above does not work, use this instead
from . import file
Upvotes: 4