PATH issue with pytest 'ImportError: No module named YadaYadaYada'

前端 未结 20 2674
孤独总比滥情好
孤独总比滥情好 2020-11-22 07:10

I used easy_install to install pytest on a mac and started writing tests for a project with a file structure likes so:

repo/
repo/app.py
repo/settings.py
rep         


        
20条回答
  •  轮回少年
    2020-11-22 07:36

    Yes, the source folder is not in Python's path if you cd to the tests directory.

    You have 2 choices:

    1. Add the path manually to the test files, something like this:

      import sys, os
      myPath = os.path.dirname(os.path.abspath(__file__))
      sys.path.insert(0, myPath + '/../')
      
    2. Run the tests with the env var PYTHONPATH=../.

提交回复
热议问题