question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ImportMismatchError from conftest.py confusion

See original GitHub issue

With a recent release of scikit-learn, we have started to see the following error when building packages for Debian using pytest 4.6.9 with Python 3.8 on Debian unstable:

/usr/lib/python3/dist-packages/_pytest/config/__init__.py:440: in _importconftest
    return self._conftestpath2mod[conftestpath]
E   KeyError: local('/build/scikit-learn-0.23.0/sklearn/conftest.py')

The package build happens roughly as follows:

  1. python3.8 setup.py config
  2. python3.8 setup.py build with a target directory .pybuild/cpython3_3.8/build/sklearn, which is a subdirectory within the source directory
  3. cd .pybuild/cpython3_3.8/build/sklearn && python3.8 -m pytest

After step (2.), find . -name conftest.py in the source directory produces the following output (sklearn being the source directory, .pybuild the build directory:

./doc/conftest.py
./.pybuild/cpython3_3.8/build/sklearn/conftest.py
./.pybuild/cpython3_3.8/build/sklearn/externals/conftest.py
./.pybuild/cpython3_3.8/build/sklearn/utils/tests/conftest.py
./.pybuild/cpython3_3.8/build/sklearn/datasets/tests/conftest.py
./sklearn/conftest.py
./sklearn/externals/conftest.py
./sklearn/utils/tests/conftest.py
./sklearn/datasets/tests/conftest.py

Running step (3.) produces the following output:

===================================================== test session starts =====================================================
platform linux -- Python 3.8.3, pytest-4.6.9, py-1.8.1, pluggy-0.13.0 -- /usr/bin/python3.8
cachedir: .pytest_cache
rootdir: /build/scikit-learn-0.23.0, inifile: setup.cfg
collected 0 items / 1 errors                                                                                                  

=========================================================== ERRORS ============================================================
________________________________________________ ERROR collecting test session ________________________________________________
/usr/lib/python3/dist-packages/_pytest/config/__init__.py:440: in _importconftest
    return self._conftestpath2mod[conftestpath]
E   KeyError: local('/build/scikit-learn-0.23.0/sklearn/conftest.py')

During handling of the above exception, another exception occurred:
/usr/lib/python3/dist-packages/_pytest/config/__init__.py:446: in _importconftest
    mod = conftestpath.pyimport()
/usr/lib/python3/dist-packages/py/_path/local.py:721: in pyimport
    raise self.ImportMismatchError(modname, modfile, self)
E   py._path.local.LocalPath.ImportMismatchError: ('sklearn.conftest', '/build/scikit-learn-0.23.0/.pybuild/cpython3_3.8/build/sklearn/conftest.py', local('/build/scikit-learn-0.23.0/sklearn/conftest.py'))

During handling of the above exception, another exception occurred:
/usr/lib/python3/dist-packages/py/_path/common.py:383: in visit
    for x in Visitor(fil, rec, ignore, bf, sort).gen(self):
/usr/lib/python3/dist-packages/py/_path/common.py:435: in gen
    for p in self.gen(subdir):
/usr/lib/python3/dist-packages/py/_path/common.py:435: in gen
    for p in self.gen(subdir):
/usr/lib/python3/dist-packages/py/_path/common.py:424: in gen
    dirs = self.optsort([p for p in entries
/usr/lib/python3/dist-packages/py/_path/common.py:425: in <listcomp>
    if p.check(dir=1) and (rec is None or rec(p))])
/usr/lib/python3/dist-packages/_pytest/main.py:667: in _recurse
    ihook = self.gethookproxy(dirpath)
/usr/lib/python3/dist-packages/_pytest/main.py:482: in gethookproxy
    my_conftestmodules = pm._getconftestmodules(fspath)
/usr/lib/python3/dist-packages/_pytest/config/__init__.py:424: in _getconftestmodules
    mod = self._importconftest(conftestpath.realpath())
/usr/lib/python3/dist-packages/_pytest/config/__init__.py:463: in _importconftest
    raise ConftestImportFailure(conftestpath, sys.exc_info())
E   _pytest.config.ConftestImportFailure: (local('/build/scikit-learn-0.23.0/sklearn/conftest.py'), (<class 'py._path.local.LocalPath.ImportMismatchError'>, ImportMismatchError('sklearn.conftest', '/build/scikit-learn-0.23.0/.pybuild/cpython3_3.8/build/sklearn/conftest.py', local('/build/scikit-learn-0.23.0/sklearn/conftest.py')), <traceback object at 0x7f989924cc00>))

I saw that rootdir pointed to the source directory, and not the build directory (the cwd at this time). I assumed this was because the only copy of setup.cfg resided there. I copied that file to the build directory and ran pytest with --rootdir=$PWD, but that didn’t change the outcome.

Oddly enough, removing the following two files from the source directory (not the build directory) resolves the issue.

./sklearn/conftest.py
./sklearn/datasets/tests/conftest.py

The other two conftest.py files in the source directory did not affect the outcome.

What could be going on here?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
RonnyPfannschmidtcommented, May 18, 2020

if you create a temporary copy of the package in a folder that is traversed by pytest for test finding, then its going to try and import the folders,

pytest auto-adds package roots of python files it finds to sys.path to import them

as far as i can tell, ./.pybuild/cpython3_3.8/build is discovered as import root and shares structure with . - in turn causing the confusion

0reactions
ckastnercommented, May 18, 2020

as far as i can tell, ./.pybuild/cpython3_3.8/build is discovered as import root and shares structure with . - in turn causing the confusion

That confusion on behalf of pytest I would understand and expect.

However, in this particular instance, ./.pybuild/cpython3_3.8/build is the current working directory (there was a cd into it). So the confusion comes from ./sklearn and ../../../sklearn.

It’s the latter that I don’t understand. Is it not possible (via an option) to cut off discovery at some base dir? I looked at rootdir (which didn’t work) and confcutdir (which worked for just one of the two conflicting files – the other still raises ImportMismatchError.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting error ImportMismatchError while running py.test
I am using Python 3.6. In my case, I was getting ImportMismatchError in modules with the same name under different packages, e.g., ...
Read more >
get rid of "ImportMismatchError" · Issue #2042 · pytest-dev ...
ImportMismatchError with files under .tox tox-dev/tox#373 ... tests/conftest.py') During handling of the above exception, another exception ...
Read more >
How to fix ImportMismatchError in Python - Nicolas Bouliane
If you get this error, it's probably a very easy fix. Here's how to fix this error with a single terminal command.
Read more >
1390573 - Clean up stale pyc files during Vagrant provision
pyc files during provision To prevent confusing pytest `ImportMismatchError` exceptions if the paths hardcoded in pyc files (which are git-ignored) leftover ...
Read more >
1.9.0 (2020-06-24) — py 1.9.1.dev3+g2da2cae documentation
If you use py in conjunction with a type checker, and encounter any type errors ... variable that suppresses ImportMismatchError exceptions when set...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found