Skip to content

Windows virtual environment does not work correctly under WSL #124139

Open

Description

@pwnfooj716

Bug report

Bug description:

Currently, Windows venvs work under cygwin because the activate script checks for it and converts the path as necessary. Cygwin is old and the new way to run a Linux shell on Windows is through the Windows Subsystem for Linux (WSL). However, there is no check for it in the script, making it not work correctly.

The relevant snippet from the script is:

# on Windows, a path can contain colons and backslashes and has to be converted:
case "$(uname)" in
    CYGWIN*|MSYS*)
        # transform D:\path\to\venv to /d/path/to/venv on MSYS
        # and to /cygdrive/d/path/to/venv on Cygwin
        VIRTUAL_ENV=$(cygpath "__VENV_DIR__")
        export VIRTUAL_ENV
        ;;
    *)
        # use the path as-is
        export VIRTUAL_ENV="__VENV_DIR__"
        ;;
esac

I was able to easily fix it manually by changing the reference to my project's venv path with $(wslpath -u "__VENV_DIR__"). All we need is a check for WSL in the script and another prong on this case statement. The best way to do this is probably by searching for "microsoft" or "wsl" in the output of uname -r. Some work might be needed to ensure this covers all edge cases.

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Linked PRs

Activity

  1. added
    type-bug
    on Sep 16, 2024
  2. added
    topic-repl
    on Aug 15, 2025
  3. gaborbernat commented on Jun 5, 2026

    @gaborbernat
    Contributor

    The robust fix is the approach virtualenv takes: derive VIRTUAL_ENV from the activate script's own location rather than detecting the shell and converting a baked-in path. virtualenv falls back to realpath "${BASH_SOURCE[0]}/../" when the recorded path isn't a directory, and only runs cygpath for Cygwin/MSYS/MINGW — never WSL: activate.sh L62-77.

    That keys on "does the recorded path resolve here", which is the correct axis. A native Linux venv created in WSL keeps its POSIX path untouched, and a Windows venv sourced from WSL via its /mnt/... mount resolves to a working path with no wslpath and no WSL detection. Detecting WSL and calling wslpath instead converts unconditionally, so it also rewrites native Linux venvs created inside WSL.

    One caveat regardless of approach: activation only sets VIRTUAL_ENV/PATH. The venv's Scripts/* shebangs and pyvenv.cfg home stay absolute, so a cross-mounted Windows venv still won't run its console scripts from WSL. The script-relative derivation is the right mechanism for the activation layer; the rest is a deeper, separate limitation.

    (Also: this is labelled topic-repl; it looks like it should be topic-venv.)

  4. added
    topic-venv
    and removed
    topic-repl
    on Jun 5, 2026
  5. zooba commented on Jun 5, 2026

    @zooba
    Member

    It's also not Windows (WSL is a Linux virtual machine, so fixes belong to the Linux build, not the Windows build - at most, it's "generic"). And to contrast, Cygwin is Windows, but is also unsupported and largely relies on using their fork of CPython.

    We made the Powershell activation script avoid using any embedded data so that it can be signed, perhaps there's value in making the other scripts similarly static?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic-venvtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions