pytest module import ImportError: No module named

最后都变了- 提交于 2019-12-12 15:27:42

问题


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:

  1. Using -m to import myapplib or myapp.myapplib
  2. Starting pytest from a different dir (e.g. inside tests/)
  3. Using all the path hacks in all the other answers
  4. Changing it to from . import XXX or .. or any of the relative import options (but these all fail with ValueError: 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


回答1:


you're running

myapp/tests/test_stuff.py

you need to edit the path in test_stuff.py to point to myapp (which contains __init__.py)

I tested this on my system (file test_stuff.py):

import sys,os
sys.path.append(os.path.join(os.path.dirname(__file__),os.pardir,"myapp"))
import myapplib

(getting full path of your python module, then using parent dir to add the python path to just the second level of myapp)

works on my dirtree configured like yours:

S:\python\myapp\myapp
S:\python\myapp\tests
S:\python\myapp\myapp\myapplib
S:\python\myapp\myapp\myapplib\__init__.py
S:\python\myapp\tests\test_stuff.py

Advice: when fiddling with sys.path.append():

  • always use absolute paths, using __file__ to make sure that the current directory doesn't get in the way. never depend on the current directory, or this trick will only work when in a given directory.
  • debug: print the path you're adding to check if it's the proper one. Since it's absolute, you'll easily see if it's correct.


来源:https://stackoverflow.com/questions/48694708/pytest-module-import-importerror-no-module-named

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!