I\'m trying to follow PEP 328, with the following directory structure:
pkg/
__init__.py
components/
core.py
__init__.py
tests/
core_test.py
If your use case is for running tests, and it seams that it is, then you can do the following. Instead of running your test script as python core_test.py
use a testing framework such as pytest
. Then on the command line you can enter
$$ py.test
That will run the tests in your directory. This gets around the issue of __name__
being __main__
that was pointed out by @BrenBarn. Next, put an empty __init__.py
file into your test directory, this will make the test directory part of your package. Then you will be able to do
from ..components.core import GameLoopEvents
However, if you run your test script as a main program then things will fail once again. So just use the test runner. Maybe this also works with other test runners such as nosetests
but i haven't checked it. Hope this helps.