3

I am using PyTest, but it can't seem to run my import statements.

My directory structures is like this:

-[app]
   -script1.py
-[tests]
   -test1.py

My test1.py file includes this line:

from script1 import my_func

and I get an error like ModuleNotFoundError: No module named script1 when I run pytest from the command line.

I tried adding this statement to cd the directory above but it didn't work:

import os
os.chdir('..')
max
  • 4,141
  • 5
  • 26
  • 55
  • does this help? https://stackoverflow.com/q/26804421/5986907 – joel Sep 26 '18 at 13:58
  • `PYTHONPATH=app/ pytest tests/test1.py` should work, or (if using Python 3) add an empty `conftest.py` file in the project root (along the dirs `app` and `tests`) and change the import to `from app.script1 import my_func`; `app` is then treated as an implicit namespace package. – hoefling Sep 26 '18 at 14:37

1 Answers1

-2

Like you specified. script1.py module is in [app] So it should be

from app.script1 import my_func

or if you want to import all functions from script.py

import app.script1 

and just call any function

script1.anyfunction()