Saturday, December 29, 2018

python - How to fix "Attempted relative import in non-package" even with __init__.py

It depends on how you want to launch your script.



If you want to launch your UnitTest from the command line in a classic way, that is:



python tests/core_test.py


Then, since in this case 'components' and 'tests' are siblings folders, you can import the relative module either using the insert or the append method of the sys.path module.

Something like:



import sys
from os import path
sys.path.append( path.dirname( path.dirname( path.abspath(__file__) ) ) )
from components.core import GameLoopEvents


Otherwise, you can launch your script with the '-m' argument (note that in this case, we are talking about a package, and thus you must not give the '.py' extension), that is:




python -m pkg.tests.core_test


In such a case, you can simply use the relative import as you were doing:



from ..components.core import GameLoopEvents


You can finally mix the two approaches, so that your script will work no matter how it is called.
For example:




if __name__ == '__main__':
if __package__ is None:
import sys
from os import path
sys.path.append( path.dirname( path.dirname( path.abspath(__file__) ) ) )
from components.core import GameLoopEvents
else:
from ..components.core import GameLoopEvents

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...