Skip to content Skip to sidebar Skip to footer

Pytest Module Import Importerror: No Module Named

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. Structu

Solution 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\myappS:\python\myapp\testsS:\python\myapp\myapp\myapplibS:\python\myapp\myapp\myapplib\__init__.pyS:\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.

Post a Comment for "Pytest Module Import Importerror: No Module Named"