pytest-dev / pytest-dev/pytest

pytest ignores `-P` (avoid prepend) argument provided to Python

Open
#12,546 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Python 3.11, Windows 11, pytest==8.2.2

  1. Create new_test.py in your current folder.
import sys
print(sys.path)
  1. Run py -P -m pytest new_test.py --capture=no
C:\numpy> py -P -m pytest new_test.py --capture=no
=============================================================================================================== test session starts ===============================================================================================================
platform win32 -- Python 3.11.7, pytest-8.2.2, pluggy-1.5.0
rootdir: C:\\numpy
configfile: pytest.ini
collecting ... ['C:\numpy', 'C:\numpy\\build-install\\usr\\Lib\\site-packages', 'Python311\\python311.zip', 'Python311\\DLLs', 'Python311\\Lib', 'Python311', 'Python311\\Lib\\site-packages', 'Python311\\Lib\\site-packages\\win32', 'Python311\\Lib\\site-packages\\win32\\lib', 'Python311\\Lib\\site-packages\\Pythonwin']
collected 0 items
  1. As you can notice C:\numpy is present in sys.path now, therefore import numpy will import numpy not from C:\numpy\\build-install\\usr\\Lib\\site-packages as it's set by PYTHONPATH environment variable but from C:\numpy which is not intended since -P is provided: https://github.com/numpy/numpy/issues/26816

Example of how -P option usually works

py -P new_test.py   
['C:\numpy\\build-install\\usr\\Lib\\site-packages', 'Python311\\python311.zip', 'Python311\\DLLs', 'Python311\\Lib', 'Python311', 'Python311\\Lib\\site-packages', 'Python311\\Lib\\site-packages\\win32', 'Python311\\Lib\\site-packages\\win32\\lib', 'Python311\\Lib\\site-packages\\Pythonwin']

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

Reproduce the Windows command py -P -m pytest new_test.py --capture=no and compare its sys.path with py -P new_test.py. Trace pytest's module entry point and startup path handling; done means pytest preserves Python's -P behavior so the current directory is not added to sys.path.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.