pytest cannot import module while python can

pytest cannot import module while python can

Found the answer:

DO NOT put a __init__.py file in a folder containing TESTS if you plan on using pytest. I had one such file, deleting it solved the problem.

This was actually buried in the comments to the second answer of PATH issue with pytest ImportError: No module named YadaYadaYada so I did not see it, hope it gets more visibility here.

I cant say I understand why this works, but I had the same problem and the tests work fine if I run python -m pytest.

Im in a virtualenv, with pytest also available globally:

(proj)[email protected] ~/dev/proj$ type -a python
python is /home/tom/.virtualenvs/proj/bin/python
python is /usr/bin/python

(proj)[email protected] ~/dev/proj$ python -V
Python 3.5.2

(proj)[email protected] ~/dev/proj$ type -a pytest
pytest is /home/tom/.virtualenvs/proj/bin/pytest
pytest is /usr/bin/pytest

(proj)[email protected] ~/dev/proj$ pytest --version
This is pytest version 3.5.0, imported from /home/tom/.virtualenvs/proj/lib/python3.5/site-packages/pytest.py

pytest cannot import module while python can

I just solved this by removing the __init__.py in my project root:

.
├── __init__.py <--- removed
├── models
│   ├── __init__.py
│   ├── address.py
│   ├── appointment.py
│   └── client.py
├── requirements.txt
├── setup.cfg
├── tests
│   ├── __init__.py
│   ├── models
│   │   ├── __init__.py
│   │   ├── appointment_test.py
│   │   └── client_test.py
│   └── other_test.py
└── script.py

Leave a Reply

Your email address will not be published. Required fields are marked *