2

When I try to use Pytest and Streamlit in the same project, things go wrong in the module imports, and I'm not sure how to handle it.

My directory is as follows:

repo
├── main.py
├── app.py
├── utils
│   ├── __init__.py
│   ├── util_a.py
└── tests
     ├── __init__.py
     └── test_a.py

The app.py file includes the next import line:

import utils.util_a 
...

Now, the streamlit application runs as expected with the following line: streamlit run repo/app.py

But while I run py.test I received an error message:

ModuleNotFoundError: No module named 'utils'

If I replace the import line in app.py as follow:

import repo.utils.util_a 
...

Surprisingly, Pytest runs as expected but the Streamlit application returns the next error:

ModuleNotFoundError: No module named 'repo'

What is the root cause of that error, and how can I deal with it? How can I use Pytest and Streamlit in the same project without causing an import conflict?

Thank you!

Yanirmr
  • 923
  • 8
  • 25
  • https://stackoverflow.com/a/50610630/11079284 That answer about `conftest.py` solves the problem. – Yanirmr Dec 27 '21 at 09:57

1 Answers1

2

Add conftest.py file in the repo solves the problem. Now, the directory looks like this:

repo
├── main.py
├── app.py
├── conftest.py
├── utils
│   ├── __init__.py
│   ├── util_a.py
└── tests
     ├── __init__.py
     └── test_a.py

More details in that post

Yanirmr
  • 923
  • 8
  • 25