I’ve a question on importing a python directory foo as a module. The structure is:
—
foo—
__init__.py
—file1.py
—file2.py
__init__.py contains the following line:
__all__ = ["file1", "file2"]
What I would want to do is to import foo and then access file1 as foo.file1
However, I can only access file1 after doing:
from foo import *
There’s a way by which I can access file1 through foo.file1, it is:
from foo import *
import foo
As you can see this is quite inefficient. Can someone point me to what is the right way to do what I am aiming to do.
Thanks
That’s exactly what the documentation says that
__all__does. If you want to be able to dofoo.file1, instead of using__all__, do this in__init__.py: