pytest fails for source installs that don't use `pip install --editable .`
See original GitHub issueStrangely it seems that if we install from source with
python -m pip install .[test]
pytest
will fail with things like
==================================== ERRORS ====================================
_____________________ ERROR collecting src/pyhf/compat.py ______________________
/opt/hostedtoolcache/Python/3.8.10/x64/lib/python3.8/site-packages/_pytest/runner.py:311: in from_call
result: Optional[TResult] = func()
/opt/hostedtoolcache/Python/3.8.10/x64/lib/python3.8/site-packages/_pytest/runner.py:341: in <lambda>
call = CallInfo.from_call(lambda: list(collector.collect()), "collect")
/opt/hostedtoolcache/Python/3.8.10/x64/lib/python3.8/site-packages/_pytest/doctest.py:532: in collect
module = import_path(self.fspath)
/opt/hostedtoolcache/Python/3.8.10/x64/lib/python3.8/site-packages/_pytest/pathlib.py:544: in import_path
raise ImportPathMismatchError(module_name, module_file, path)
E _pytest.pathlib.ImportPathMismatchError: ('pyhf.compat', '/opt/hostedtoolcache/Python/3.8.10/x64/lib/python3.8/site-packages/pyhf/compat.py', PosixPath('/home/runner/work/pyhf/pyhf/src/pyhf/compat.py'))
and that it will only pass if we install from source in editable mode
python -m pip install --editable .[test]
_Originally posted by @matthewfeickert in https://github.com/scikit-hep/pyhf/issues/1453#issuecomment-841541604_
It would be good to understand why this is, and if possible change it. The tests needing things to be installed with pip install --editable .
seems strange and bad.
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (12 by maintainers)
Top Results From Across the Web
When would the -e, --editable option be useful with pip install?
This allows your project to be both installed and editable in project form. Assuming you're in the root of your project directory, then...
Read more >22.1: Editable installs for packages with extras & flit causes ...
When I run with --debug , I see that the dev extra is being passed in as an environment marker, which then fails...
Read more >Good Integration Practices — pytest documentation
If you don't use an editable install and are relying on the fact that Python by default puts the current directory in sys.path...
Read more >Using Python's pip to Manage Your Projects' Dependencies
In this beginner-friendly tutorial, you'll learn how to use pip, the standard ... Installing Packages in Editable Mode to Ease Development.
Read more >Installing the development version of scikit-learn
This section introduces how to install the main branch of scikit-learn. This can be done by either installing a nightly build or building...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I spent an hour or so digging into this and I found some useful info. pytest pulls in
tests/conftest.py
as part of its first pass, before it loads any other test files or source code, and in particular, before it addssrc/
tosys.path
for doctest discovery. Sinceconftest.py
importspyhf
, it will load the versions of the modules from the installed version of the package, i.e.lib/pythonX.Y/site-packages/pyhf/
. Then later, when pytest goes looking for (doc)tests undersrc/
, it prependssrc/
tosys.path
(NB for doctests, it always prepends, ignoring the--import-mode
CLI option) in an effort to import the.py
files from withinsrc/
, but those modules already exist insys.modules
, having been imported from the installed version. The conflict between the already-imported module and the file that pytest is trying to newly import seems to cause the mismatch error.So ultimately, if you can remove the dependency of
tests/conftest.py
on thepyhf
code, that should do the trick. I did a quick “feasibility study” by removing the whole parameter list ofbackend()
and thepyhf
import, and it seems to allow things to proceed past test collection, which is a good sign, though I did get a test to fail so maybe there are other things to fix.I’m not sure (need to test) but I think it is because we have
src
in ourtestpaths
inpyproject.toml
https://github.com/scikit-hep/pyhf/blob/546022254a526ea62af8cbc6594ec881cddfa9f1/pyproject.toml#L41-L44