pytest

`pytest` behaves differently from `python -m pytest` and add parent folder to `sys.path[0]` instead


Using Windows 11. I wrote a test script and put it in c:\coding\example\tests\test_script.py.
Here is its content:

import sys
print(sys.path[0])

When I run pytest from c:\coding\example I get c:\coding.
When I run python -m pytest from c:\coding\example I get c:\coding\example

Additional info:
where python returns: C:\Users\atur.sams\AppData\Local\Programs\Python\Python313\python.exe
where pytest returns: C:\Users\atur.sams\AppData\Local\Programs\Python\Python313\Scripts\pytest.exe

Is this normal? What is causing it?

A good answer would clarify and cause and if possible a way to make pytest behave like python -m pytest.


Solution

  • Is this normal? What is causing it?

    Calling via python -m pytest adds the current directory to sys.path.

    See: https://docs.pytest.org/en/stable/how-to/usage.html#other-ways-of-calling-pytest