Skip to content

Support venv detection on Windows with mingw Python #12544

Closed
@lazka

Description

@lazka

What's the problem this feature will solve?

Currently pytest checks platform depended paths in a directory to see if it might be a venv:

pytest/src/_pytest/main.py

Lines 370 to 387 in 0ed2d79

def _in_venv(path: Path) -> bool:
"""Attempt to detect if ``path`` is the root of a Virtual Environment by
checking for the existence of the appropriate activate script."""
bindir = path.joinpath("Scripts" if sys.platform.startswith("win") else "bin")
try:
if not bindir.is_dir():
return False
except OSError:
return False
activates = (
"activate",
"activate.csh",
"activate.fish",
"Activate",
"Activate.bat",
"Activate.ps1",
)
return any(fname.name in activates for fname in bindir.iterdir())

This works fine with the official CPython releases, but the mingw Python on Windows uses the Unix layout, which looks like this, despite being on Windows:

$ tree -L 1 .venv
.venv
├── bin
├── include
├── lib
└── pyvenv.cfg

This results in pytest trying to collect tests in venv files by default.

Describe the solution you'd like

I can see three solutions to this:

  1. Check for both bin/Scripts

  2. First check for the existence of "pyvenv.cfg", which should be in every venv on every platform

  3. Replace the current logic and just check for "pyvenv.cfg". "pyvenv.cfg" was introduced in Python 3.3 with the venv module, so it might be enough to assume it exists in every venv. Maybe I'm missing something though.

Note: I'm happy to create a PR for any of the above solutions.

Activity

RonnyPfannschmidt

RonnyPfannschmidt commented on Jun 28, 2024

@RonnyPfannschmidt
Member

the initial code was added in b32cfc8 about 7 years ago when python 2.6 was still supported

so as far as i'm aware there is no reason not to switch to this much simpler test - a pr would be appreciated

bluetech

bluetech commented on Jun 28, 2024

@bluetech
Member

Agreed, as far as I know, checking for pyvenv.cfg is sufficient.

zachsnicker

zachsnicker commented on Jun 28, 2024

@zachsnicker
Contributor

Hello!
I'm new to contributing to pytest. Can I take this up?

webknjaz

webknjaz commented on Jun 28, 2024

@webknjaz
Member

@zachsnicker I think it's non-controversial enough, I'll assign you. Make sure to include test coverage for the patch in your pull request.

lazka

lazka commented on Jun 28, 2024

@lazka
Author

Just to be safe I did some additional research, https://peps.python.org/pep-0405/ defines venvs and states "Thus, a Python virtual environment in its simplest form would consist of nothing more than a copy or symlink of the Python binary accompanied by a pyvenv.cfg file and a site-packages directory.". So it's safe to assume that every venv following the spec will contain a pyvenv.cfg.

For the external virtualenv package pyvenv.cfg was added in v20, released 4+ years ago, which is also in Ubuntu 20.04 for example.

So I think this is fine.

I also had a look at the code already, so I'm happy to review if wanted.

added a commit that references this issue on Jun 29, 2024
0adcc21
added a commit that references this issue on Jul 30, 2024
2819b4d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

good first issueeasy issue that is friendly to new contributorplatform: mingwtype: bugproblem that needs to be addressed

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @RonnyPfannschmidt@webknjaz@lazka@bluetech@zachsnicker

    Issue actions

      Support venv detection on Windows with mingw Python · Issue #12544 · pytest-dev/pytest