my_project
-my_project
- __init__.py
- main.py
- constants.py
-test
- __init__.py
- test_main.py
test_main.py from my_project import main
main.py import constants
When I run nosetests in my_project, I end up getting ImportError: No module named 'constants'
Both __init__.py files are blank.
If I change import constants to from my_project import constants in main.py, nosetests work. However, now if I just run python main.py I get ImportError: No module named 'my_project'.
Can someone help me point out what I'm doing wrong? I've searched quite a few posts but I haven't been able to fix it myself. Thanks.
In main.py ->
import constantsis an implicit relative import (bad). It should be changed to the more usualfrom my_project import constants.You mentioned this makes nosetests work. Note: you don't need the
__init__.pyin the tests sub-directory, and in fact this is usually discouraged.Now to fix your error with
python main.pyhaving an import error, well that is normal if you haven't included it in yoursys.path. There are various ways around this -