pytest
pytest copied to clipboard
pytest ignores `-P` (avoid prepend) argument provided to Python
Python 3.11, Windows 11, pytest==8.2.2
- Create new_test.py in your current folder.
import sys
print(sys.path)
- 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
- As you can notice
C:\numpyis present insys.pathnow, thereforeimport numpywill importnumpynot fromC:\numpy\\build-install\\usr\\Lib\\site-packagesas it's set byPYTHONPATHenvironment variable but fromC:\numpywhich is not intended since-Pis 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']
The pytest import mode is independent from python and configured with a different option
In general its recommended to use the pytest - command to avoid python adding cwd to sys.path
The pytest import mode is independent from python and configured with a different option In general its recommended to use the pytest - command to avoid python adding cwd to sys.path
What would be the recommended way to avoid pytest adding cwd to sys.path?