pythonpytest

Py.test No module named *


I have a folder structure like this

App
--App
  --app.py       
--Docs
--Tests
  --test_app.py

In my test_app.py file, I have a line to import my app module. When I run py.test on the root folder, I get this error about no module named app. How should I configure this?


Solution

  • So you are running py.test from /App. Are you sure /App/App is in your $PYTHONPATH?

    If it's not, code that tries to import app will fail with such a message.

    EDIT0: including the info from my comment below, for completeness.

    An attempt to import app will only succeed if it was executed inside /App/App, which is not the case here. You probably want to make /App/App a package by putting __init__.py inside it, and change your import to qualify app as from App import app.

    EDIT1: by request, adding further explanation from my second comment below.

    By putting __init__.py inside /App/App, that directory becomes a package. Which means you can import from it, as long as it - the directory - is visible in the $PYTHONPATH. I.e. you can do from App import app if /App is in the $PYTHONPATH. Your current working directory gets automatically added to $PYTHONPATH, so when you run a script from /App, the import will work.