I've read a few Python relative vs. absolute import tutorials and can't figure out this ModuleNotFound
error for the life of me.
I'm working with the following directory structure:
project
|
+-- pseudo
| |
| +-- __main__.py
| |
| +-- pseudo.py
| |
| +-- analytics_generator
| |
| +-- analytics_generator.py
| |
| +-- models
| |
| +-- blueprint.py
The root of the problem is that in the analytics_generator.py file, I'm trying to import SomeClass from blueprint.py.
When I execute the main function in __main__.py
, I get the following error:
Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1741, in <module>
main()
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1735, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1135, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File ".../project/pseudo/__main__.py", line 2, in <module>
from pseudo import Pseudo
File ".../project/pseudo/pseudo.py", line 4, in <module>
from analytics_generator.analytics_generator import AnalyticsGenerator
File ".../project/pseudo/analytics_generator/analytics_generator.py", line 1, in <module>
from models.blueprints import SomeClass
ModuleNotFoundError: No module named 'models'
I'm running the script within Pycharm and my working directory is .../project/pseudo
In the analytics_generator.py file, if I change the import statement to a relative import it works: from .models.blueprints import SomeClass
.
However, using the full path doesn't:
from pseudo.analytics_generator.models.blueprints import SomeClass
throws:
ModuleNotFoundError: No module named 'pseudo.analytics_generator'; 'pseudo' is not a package
Any guidance is much appreciated!