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

Fix path resolution for python executables looked up in PATH on windows #1232

Merged
merged 4 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/1205.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix path resolution for python executables looked up in PATH on windows.
2 changes: 2 additions & 0 deletions src/pipx/shared_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def create(self, verbose: bool = False) -> None:
[DEFAULT_PYTHON, "-m", "venv", "--clear", self.root], run_dir=str(self.root)
)
subprocess_post_check(create_process)
# Recompute these paths, as they might resolve differently now, see comment in get_venv_paths
self.bin_path, self.python_path, self.man_path = get_venv_paths(self.root)

# ignore installed packages to ensure no unexpected patches from the OS vendor
# are used
Expand Down
12 changes: 4 additions & 8 deletions src/pipx/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ def run_pypackage_bin(bin_path: Path, args: List[str]) -> NoReturn:
if WINDOWS:

def get_venv_paths(root: Path) -> Tuple[Path, Path, Path]:
# Make sure to use the real root path. This matters especially on Windows when using the packaged app
# (Microsoft Store) version of Python, which uses path redirection for sandboxing.
# See https://github.com/pypa/pipx/issues/1164
root = root.resolve()
bin_path = root / "Scripts" if not MINGW else root / "bin"
python_path = bin_path / "python.exe"
man_path = root / "share" / "man"
Expand Down Expand Up @@ -172,14 +176,6 @@ def run_subprocess(
os.makedirs(run_dir, exist_ok=True)
# windows cannot take Path objects, only strings
cmd_str_list = [str(c) for c in cmd]
# Make sure to call the binary using its real path. This matters especially on Windows when using the packaged app
# (Microsoft Store) version of Python, which uses path redirection for sandboxing. If the path to the executable is
# redirected, the executable can get confused as to which directory it's being run from, leading to problems.
# See https://github.com/pypa/pipx/issues/1164
# Conversely, if the binary is a symlink, then we should NOT use the real path, as Python expects to receive the
# symlink in argv[0] so that it can locate the venv.
if not os.path.islink(cmd_str_list[0]) and WINDOWS:
Gitznik marked this conversation as resolved.
Show resolved Hide resolved
cmd_str_list[0] = os.path.realpath(cmd_str_list[0])

# TODO: Switch to using `-P` / PYTHONSAFEPATH instead of running in
# separate directory in Python 3.11
Expand Down
26 changes: 0 additions & 26 deletions tests/test_util.py

This file was deleted.