pytest icon indicating copy to clipboard operation
pytest copied to clipboard

`pytester`: setting `HOME` to `tmpdir` causes `pytest` no to be found

Open superlevure opened this issue 1 year ago • 0 comments

https://github.com/pytest-dev/pytest/pull/4941 caused HOME to be set to tmpdir when invoking pytester.runpytest_subprocess().

Because of this, if pytest is installed in USER_SITE, it will not be found when running pytester:

-------------------------------------------------------------------------------------- Captured stdout call --------------------------------------------------------------------------------------
running: /usr/local/bin/python -mpytest --basetemp=/tmp/pytest-of-vscode/pytest-0/test_valid_input_success0/runpytest-0
     in: /tmp/pytest-of-vscode/pytest-0/test_valid_input_success0
-------------------------------------------------------------------------------------- Captured stderr call --------------------------------------------------------------------------------------
/usr/local/bin/python: No module named pytest

This issue can be seen by invoking python -m site from both environments:

  • From the host:
sys.path = [
    '/workspace/app',
    '/usr/local/lib/python310.zip',
    '/usr/local/lib/python3.10',
    '/usr/local/lib/python3.10/lib-dynload',
    '/home/vscode/.local/lib/python3.10/site-packages',
    '/usr/local/lib/python3.10/site-packages',
]
USER_BASE: '/home/vscode/.local' (exists)
USER_SITE: '/home/vscode/.local/lib/python3.10/site-packages' (exists)
ENABLE_USER_SITE: True

--> '/home/vscode/.local/lib/python3.10/site-packages is where pytest (and all other packages) are installed

  • From pytester
sys.path = [
    '/tmp/pytest-of-vscode/pytest-10/test_valid_input_success0',
    '/usr/local/lib/python310.zip',
    '/usr/local/lib/python3.10',
    '/usr/local/lib/python3.10/lib-dynload',
    '/usr/local/lib/python3.10/site-packages',
]
USER_BASE: '/tmp/pytest-of-vscode/pytest-10/test_valid_input_success0/.local' (doesn't exist)
USER_SITE: '/tmp/pytest-of-vscode/pytest-10/test_valid_input_success0/.local/lib/python3.10/site-packages' (doesn't exist)
ENABLE_USER_SITE: True

Indeed:

➜  app git:(main) ✗ HOME=/tmp/pytest-of-vscode/pytest-10/test_valid_input_success0 python -m pytest --version
/usr/local/bin/python: No module named pytest

One workaround is to run pytest with:

PYTHONPATH=/home/vscode/.local/lib/python3.10/site-packages pytest .

Pytest version: 7.4.4

superlevure avatar Jan 12 '24 10:01 superlevure