pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
12.04k stars 2.67k forks source link

PyTest sometimes fails to find Python modules from the environment variable PYTHONPATH #11142

Open yurivict opened 1 year ago

yurivict commented 1 year ago

We build and install Python modules into a stage directory w/in the FreeBSD ports framework. Then we run pytest in the "tests" folder of the project's working directory while PYTHONPATH points to this module installed in the stage directory.

In many cases (not always) pytest fails to find this module in PYTHONPATH.

Testcase: https://github.com/moderngl/moderngl

Full log:


cd /usr/ports/graphics/py-moderngl/work-py39/moderngl-5.8.2/tests && /usr/bin/env XDG_DATA_HOME=/usr/ports/graphics/py-moderngl/work-py39  XDG_CONFIG_HOME=/usr/ports/graphics/py-moderngl/work-py39  XDG_CACHE_HOME=/usr/ports/graphics/py-moderngl/work-py39/.cache  HOME=/usr/ports/graphics/py-moderngl/work-py39 PATH=/usr/local/libexec/ccache:/usr/ports/graphics/py-moderngl/work-py39/.bin:/home/yuri/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin PKG_CONFIG_LIBDIR=/usr/ports/graphics/py-moderngl/work-py39/.pkgconfig:/usr/local/libdata/pkgconfig:/usr/local/share/pkgconfig:/usr/libdata/pkgconfig MK_DEBUG_FILES=no MK_KERNEL_SYMBOLS=no SHELL=/bin/sh NO_LINT=YES PREFIX=/usr/local  LOCALBASE=/usr/local  CC="cc" CFLAGS="-O2 -pipe  -fstack-protector-strong -fno-strict-aliasing "  CPP="cpp" CPPFLAGS=""  LDFLAGS=" -fstack-protector-strong " LIBS=""  CXX="c++" CXXFLAGS="-O2 -pipe -fstack-protector-strong -fno-strict-aliasing  "  MANPREFIX="/usr/local" CCACHE_DIR="/tmp/.ccache" BSD_INSTALL_PROGRAM="install  -s -m 555"  BSD_INSTALL_LIB="install  -s -m 0644"  BSD_INSTALL_SCRIPT="install  -m 555"  BSD_INSTALL_DATA="install  -m 0644"  BSD_INSTALL_MAN="install  -m 444" PYTHONPATH=/usr/ports/graphics/py-moderngl/work-py39/stage/usr/local/lib/python3.9/site-packages /usr/local/bin/python3.9 -m pytest -k '' -rs -v -o addopts= 
===================================================================================== test session starts =====================================================================================
platform freebsd13 -- Python 3.9.17, pytest-7.3.1, pluggy-1.0.0 -- /usr/local/bin/python3.9
cachedir: .pytest_cache
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('/usr/ports/graphics/py-moderngl/work-py39/moderngl-5.8.2/tests/.hypothesis/examples')
Using --randomly-seed=3233284715
benchmark: 4.0.0 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)
rootdir: /usr/ports/graphics/py-moderngl/work-py39/moderngl-5.8.2
plugins: forked-1.6.0, hypothesis-6.75.9, asyncio-0.21.0, trio-0.8.0, mypy-plugins-1.11.1, cov-2.9.0, randomly-3.12.0, timeout-2.1.0, time-machine-2.10.0, rerunfailures-11.1.2, flaky-3.7.0, anyio-3.7.0, xdist-2.5.0, env-0.6.2, mock-3.10.0, subprocess-1.5.0, typeguard-4.0.0, httpserver-1.0.8, flask-0.15.1, benchmark-4.0.0
asyncio: mode=strict
collected 345 items / 1 error                                                                                                                                                                 

=========================================================================================== ERRORS ============================================================================================
________________________________________________________________________ ERROR collecting tests/test_buffer_format.py _________________________________________________________________________
ImportError while importing test module '/usr/ports/graphics/py-moderngl/work-py39/moderngl-5.8.2/tests/test_buffer_format.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/local/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
test_buffer_format.py:1: in <module>
    from moderngl.mgl import fmtdebug
E   ModuleNotFoundError: No module named 'moderngl.mgl'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
====================================================================================== 1 error in 0.89s =======================================================================================
*** Error code 2

Pytest failed to find moderngl.mgl. But a regular python executable invocation succeeds:

[yuri@yv /usr/ports/graphics/py-moderngl]$ PYTHONPATH=/usr/ports/graphics/py-moderngl/work-py39/stage/usr/local/lib/python3.9/site-packages  python3.9 x.py 
[yuri@yv /usr/ports/graphics/py-moderngl]$ cat x.py 
from moderngl.mgl import fmtdebug

pytest-7.3.1 Python-3.9 FreeBSD 13.2

yurivict commented 9 months ago

5.9.0 still has the same problem.