Skip to content

--python flag is broken on Windows #1205

@guahki

Description

@guahki

Describe the bug

The --python flag used to work with a executable name in the PATH. E.g. pipx install --python python3.12.exe ... worked, if python3.12.exe was found on the path.

Since #1168, however, this does no longer work. See analysis below.

$ where python3.12.exe
C:\Users\Dominik\AppData\Local\Microsoft\WindowsApps\python3.12.exe
$ pipx install --python python3.12.exe flit
creating virtual environment...

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\not_of_interest\Scripts\pipx.exe\__main__.py", line 7, in <module>
  File "C:\not_of_interest\Lib\site-packages\pipx\main.py", line 911, in cli
    return run_pipx_command(parsed_pipx_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\not_of_interest\Lib\site-packages\pipx\main.py", line 212, in run_pipx_command
    return commands.install(
           ^^^^^^^^^^^^^^^^^
  File "C:\not_of_interest\Lib\site-packages\pipx\commands\install.py", line 83, in install
    venv.create_venv(venv_args, pip_args, override_shared)
  File "C:\not_of_interest\Lib\site-packages\pipx\venv.py", line 160, in create_venv
    venv_process = run_subprocess(cmd + venv_args + [str(self.root)])
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\not_of_interest\Lib\site-packages\pipx\util.py", line 179, in run_subprocess
    completed_process = subprocess.run(
                        ^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.496.0_x64__qbz5n2kfra8p0\Lib\subprocess.py", line 548, in run
    with Popen(*popenargs, **kwargs) as process:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.496.0_x64__qbz5n2kfra8p0\Lib\subprocess.py", line 1026, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.496.0_x64__qbz5n2kfra8p0\Lib\subprocess.py", line 1538, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden

How to reproduce

On a windows system, pass any python executable available in the PATH.

Expected behavior

It should be possible to pass executables to be found on the PATH to the --python flag.

Analysis

The changes introduced by #1168 call os.path.realpath on the argument before giving it to subprocess.run. This causes the current working directory to be prepended and subprocess.run can not lookup the executable, as it gets a resolved/absolute (non-existing) path. Note, this is even shadowed in the verbose log, as this prints a version of the cmd, which was not passed through os.path.realpath.

See my initial direct suggestion in #1186 (comment) to fix (or at least workaround) the issue. I tried to be lazy and not open an issue, but @Gitznik rightfully told me to do so.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is neededwindows

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions