0
votes

I have a project with the following structure:

.
├── Pipfile
├── Pipfile.lock
├── rules
│   ├── __init__.py
│   ├── budget.py
│   └── tests
│       ├── __init__.py
│       └── budget_test.py

Obviously, the budget_test.py is testing the budget.py module.

from .. import budget

class TestBudget:
...

and the budget.py starts with:

import pandas as pd

When I launch the module manually, the import runs fine. When I launch pytest, I get an ImportError about pandas:

ImportError: No module named pandas

En though the virtualenv is activated and pandas is part of the installed packages:

pip freeze

... pandas==1.0.1 ...

Is there any reason pytest cannot find the packages installed in the virtual environment?

Surprisingly, running pytest fails when python -m pytest is succesful

1
@MrBeanBremen Thanks. if you put your comment as answer, I'll accept it. - E. Jaep

1 Answers

2
votes

It is common practice to use python -m pytest to be able to find all modules in the current environment. To be a bit more specific - this is needed to find modules inside packages.

You can read more about the -m switch rationale and behavior in the related PEP-338.