Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Andrej730 opened this issue Jun 29, 2024 · 3 comments
Open

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

Andrej730 opened this issue Jun 29, 2024 · 3 comments
Labels
topic: collection related to the collection phase

Comments

@Andrej730
Copy link

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: BUG: Error running spin mypy from the repository folder numpy/numpy#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']
@Andrej730 Andrej730 changed the title pytest ignores -P (avoid prepend) argument provided to the python pytest ignores -P (avoid prepend) argument provided to Python Jun 29, 2024
@RonnyPfannschmidt
Copy link
Member

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

@Andrej730
Copy link
Author

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?

@RonnyPfannschmidt
Copy link
Member

@Zac-HD Zac-HD added the topic: collection related to the collection phase label Jul 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: collection related to the collection phase
Projects
None yet
Development

No branches or pull requests

3 participants