Reputation: 101
I have a python project with the following structure
package/__init__.py
package/foo.py
package/subFolder/__init__.py
package/subFolder/myModule.py
I would like to be able to import myModule on the level of package
import package.myModule
I've tried to import the modules inside the package/__init__.py
with
from .subfolder import *
with no success...
Unfortunately I can't change the folder structure.
Upvotes: 0
Views: 119
Reputation: 613
If you want to keep the autocomplete working, you could generate the __init__.py
and fill __all__
with appropriate symbols.
Upvotes: 1
Reputation: 101
What I ended up with was to append all modules from the subFolder to the __all__
Attribute in subFolder.__init__.py
with the following snippet:
import pathlib
file_path = pathlib.Path(__file__).parent.glob('*.py')
__all__ = [path.stem for path in file_path if '__' not in path.stem]
And in package/__init__.py
:
from .generated_execution_layer import *
An ugly side effect of this is, that it broke autocompletion for this imported modules.
Upvotes: 0
Reputation: 387
If I understand your situation correctly, you have your project directory that contains your script and a sub-Folder that contains a file defining a module.
From your script in the project directory, to access your module, the following should work:
from subFolder.myModule import *
Upvotes: 0