This question has been asked DOZENS of times before, but every one I've come across doesn't have any working solutions for me.
I'm using Python 2.7 and pytest
to run tests.
Structure is as follows (from the root of my project/git repo):
myapp/
myapp/
__init__.py # def main():
# ... stuff
# if __name__ == "__main__":
# main()
__main__.py # import __init__ as myapp
# myapp.main()
config.yaml # (app config file)
myapplib/
__init__.py # (Empty file)
config.py # Loads {projectroot}/config.yaml
# def cfg():
# ... stuff
tests/
test_stuff.py # from myapplib.config import cfg
I then try to run the tests/test_stuff.py file by doing:
cd {projectroot}
pytest
And it complains:
myapp/tests/test_stuff.py:38: in <module>
from myapplib.config import cfg
E ImportError: No module named myapp.config
Some things I've tried:
- Using
-m
to importmyapplib
ormyapp.myapplib
- Starting
pytest
from a different dir (e.g. inside tests/) - Using all the path hacks in all the other answers
- Changing it to
from . import XXX
or..
or any of the relative import options (but these all fail withValueError: Attempted relative import in non-package
.
Worth nothing that the app itself executes fine from the project dir:
cd {projectroot}
python myapp
... and all config and paths work just fine. It's just the tests that can't find the path.
Here's a complete reproduction of what I have so far:
https://pyfiddle.io/fiddle/4fa60f5a-02df-43bb-8aa3-03e59ff72650/?m=Saved%20fiddle