Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytest "Error: could not load path/to/conftest.py"

Tags:

pytest

I get the following error when I try to run pytest repo/tests/test_file.py:

$ pytest repo/tests/test_file.py
Traceback (most recent call last):
  File "/Users/marlo/anaconda3/envs/venv/lib/python3.6/site-packages/_pytest/config.py", line 329, in _getconftestmodules
    return self._path2confmods[path]
KeyError: local('/Users/marlo/repo/tests/test_file.py')

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/Users/marlo/anaconda3/envs/venv/lib/python3.6/site-packages/_pytest/config.py", line 329, in _getconftestmodules
    return self._path2confmods[path]
KeyError: local('/Users/marlo/repo/tests')

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/Users/marlo/anaconda3/envs/venv/lib/python3.6/site-packages/_pytest/config.py", line 362, in _importconftest
    return self._conftestpath2mod[conftestpath]
KeyError: local('/Users/marlo/repo/conftest.py')

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/Users/marlo/anaconda3/envs/venv/lib/python3.6/site-packages/_pytest/config.py", line 368, in _importconftest
    mod = conftestpath.pyimport()
  File "/Users/marlo/anaconda3/envs/venv/lib/python3.6/site-packages/py/_path/local.py", line 686, in pyimport
    raise self.ImportMismatchError(modname, modfile, self)
py._path.local.LocalPath.ImportMismatchError: ('conftest', '/home/venvuser/venv/conftest.py', local('/Users/marlo/repo/conftest.py'))
ERROR: could not load /Users/marlo/repo/conftest.py

My repo structure is

lib/
    -tests/
        -test_file.py
app/
    -test_settings.py
pytest.ini
conftest.py
...

Other people have run this code fine, and according to this question (and this one), my structure is good and I am not missing any files. I can only conclude that something about my computer or project set-up is not right. If you have any suggestions or insights that I may be missing, please send them my way!

-------------------------------MORE DETAILS------------------------------

test_file.py:

def func(x):
    return x + 1

def test_answer():
    assert func(3) == 5

pytest.ini:

[pytest]
DJANGO_SETTINGS_MODULE = app.test_settings
python_files = tests.py test_* *_tests.py *test.py
like image 799
Marlo Avatar asked Feb 08 '18 22:02

Marlo


2 Answers

I have docker as well as running pytest outside of docker too, and for me a much lower-impact fix whenever this crops up is to delete all the compiled python files

find . -name \*.pyc -delete

like image 88
LisaD Avatar answered Oct 23 '22 18:10

LisaD


I figured it out and I'll answer in case others have the same issue:

I didn't even take into consideration that I had a docker container (of the same app) in the repo directory and, although I was not running the docker container, it was influencing the filepaths somehow.

To fix this:

  1. I re-cloned the repo from the remote source into a new folder so that nothing from the old repo could "contaminate" it.
  2. Updated my virtual environment with the .yml specifications of the clean repo

    $ conda env update --name project --file project.yml

  1. My project uses a postgres database, so I dropped it and created a new one

    $ dropdb projectdb
    $ createdb projectdb

  1. Since my project uses mongo, I also dropped that database

    $ mongo projectdb --eval "db.dropDatabase()"

  1. Installed a clean pytest

    $ pip uninstall pytest
    $ pip install pytest

...and voilà! I could run pytest.

Many thanks to @hoefling and others who helped me debug.

like image 21
Marlo Avatar answered Oct 23 '22 20:10

Marlo