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 constants
is an implicit relative import (bad). It should be changed to the more usual from my_project import constants
.
You mentioned this makes nosetests work. Note: you don't need the __init__.py
in the tests sub-directory, and in fact this is usually discouraged.
Now to fix your error with python main.py
having an import error, well that is normal if you haven't included it in your sys.path
. There are various ways around this -
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With