I'm trying to follow PEP 328, with the following directory structure:
pkg/
__init__.py
components/
core.py
__init__.py
tests/
core_test.py
__init__.py
In core_test.py
I have the following import statement
from ..components.core import GameLoopEvents
However, when I run, I get the following error:
tests$ python core_test.py
Traceback (most recent call last):
File "core_test.py", line 3, in <module>
from ..components.core import GameLoopEvents
ValueError: Attempted relative import in non-package
Searching around I found "relative path not working even with __init__.py" and "Import a module from a relative path" but they didn't help.
Is there anything I'm missing here?
unittest
projects, so I wrote this fairly exhaustive sample project that covers deep nesting of modules, relative and absolute imports (where the work and don't), and relative and absolute referencing from within a package, as well as single, double, and package-level import of classes. Helped clear things right up for me!no module named myimports.foo
when I run them.cd
intoPyImports
, and runpython -m unittest tests.test_abs
, for example.