Skip to content

Commit

Permalink
Fix path resolution for python executables looked up in PATH on windows
Browse files Browse the repository at this point in the history
by moving the whole os.path.realpath logic introduced in pypa#1168 to
pipx.util:get_venv_paths
  • Loading branch information
guahki committed Feb 2, 2024
1 parent 10cbc9c commit 2436536
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
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 @@ -45,6 +45,8 @@ def create(self, verbose: bool = False) -> None:
with animate("creating shared libraries", not verbose):
create_process = run_subprocess([DEFAULT_PYTHON, "-m", "venv", "--clear", 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 @@ -108,6 +108,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 @@ -168,14 +172,6 @@ def run_subprocess(
logger.info(f"running {log_cmd_str}")
# 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:
cmd_str_list[0] = os.path.realpath(cmd_str_list[0])
completed_process = subprocess.run(
cmd_str_list,
env=env,
Expand Down

0 comments on commit 2436536

Please sign in to comment.