scipy / scipy_doctest

Floating-point aware doctesting
BSD 3-Clause "New" or "Revised" License
5 stars 4 forks source link

Plugin name already registered & ImportPathMismatchError #92

Closed Sheila-nk closed 8 months ago

Sheila-nk commented 1 year ago

On running the command pytest --doctest-modules scipy/constants -v on the Scipy shell, you get the following error:

ValueError: Plugin name already registered: /Users/sheilakahwai/Documents/Work/scipy/build-install/lib/python3.11/site-packages/scipy/conftest.py=<module 'scipy.conftest' from '/Users/sheilakahwai/Documents/Work/scipy/build-install/lib/python3.11/site-packages/scipy/conftest.py'>

The problem seems to be with pytest's import mode: prepend which basically prepends the directory containing test files to the python path leading to conflicts with modules that have the same name but exist in different locations. You can find more information here: choosing an import mode

The issue occurs in Scipy because the build system copies the python source files to build-install.

Currently, to overcome this issue, you can either:

  1. Run doctests on the files present in build-install
    pytest --doctest-modules build-install/lib/python3.11/site-packages/scipy/constants -v 
  2. Change the import mode to importlib
    pytest --doctest-modules scipy/constants --import-mode=importlib -v

You might also come across the following error:

ImportError while loading conftest '/Users/sheilakahwai/Documents/Work/scipy/scipy/conftest.py'.
_pytest.pathlib.ImportPathMismatchError: ('scipy.conftest', '/Users/sheilakahwai/Documents/Work/scipy/build-install/lib/python3.11/site-packages/scipy/conftest.py', PosixPath('/Users/sheilakahwai/Documents/Work/scipy/scipy/conftest.py'))

This seems to be a pytest issue: https://github.com/pytest-dev/pytest/issues/9567#issuecomment-1117538794

A temporary fix is setting the environment variable: PY_IGNORE_IMPORTMISMATCH=1

ev-br commented 1 year ago

For completeness, here's the one way to run doctests and suppress these collection errors:

$ python dev.py shell
<... new shell opens>
$ PY_IGNORE_IMPORTMISMATCH=1 pytest --doctest-modules --pyargs scipy -x --ignore=/home/br/repos/scipy/scipy/build-install/lib/python3.10/site-packages/scipy/_lib --ignore=/home/br/repos/scipy/scipy/build-install/lib/python3.10/site-packages/scipy/interpolate/_interpnd_info.py

(this ignores the whole of scipy/_lib for simplicity)

ev-br commented 8 months ago

This seems to have been a combination of several things:

So this issue seems to have been fixed, let's close as completed. Thanks Sheila!