pytest-dev / pytest-dev/pytest

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

Open
#11,805 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

plugin: pytester
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with pytester.runpytest_subprocess() and reproduce the behavior by comparing python -m site with and without HOME set to the temporary directory. Done means pytester subprocesses can find pytest installed in the user's site-packages when invoking python -m pytest.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.