PyO3 / pyo3

Rust bindings for the Python interpreter
https://pyo3.rs
Apache License 2.0
12.51k stars 770 forks source link

Prefer `py` on Windows when available #2731

Open jaraco opened 2 years ago

jaraco commented 2 years ago

Did you know on Windows, the default installation options don't include installing Python on the PATH, but the default installation options do include installing the py launcher on the PATH? Perhaps pyo3 could be smarter and fall back to py -3 or simply py when no executable is specified.

I tried setting PYO3_PYTHON=py to see if a generic executable as found on the path could be supplied, so that I don't have to think about which Python executable to use, but that produced a different error:

Unable to create process using '"C:\Program Files\Python 3.11\py""': The system cannot find the file specified.

That error looks like it's revealing a couple of possible bugs:

Since the routine seemed to be able to infer the location of some Python executable, I tried setting PYO3_PYTHON=python, but that caused the Microsoft Store to launch, prompting install of Python 3.10, and pyo3 reported yet another error: error: Python script failed.

It would be nice if pyo3 could infer the location more consistently with what a user might expect on Windows, so that most users could rely on a default behavior, similar to how they can for launching Python interactively.

Originally posted by @jaraco in https://github.com/PyO3/pyo3/issues/1616#issuecomment-1305887742

messense commented 2 years ago

I think the py launcher in Python 3.11 changed some behaviors, it used to work fine when invoking with Command::new("py") in Rust, but the 3.11 py doesn't work, I had to use cmd /c py to make it work in maturin.

https://github.com/PyO3/maturin/pull/1222/files#diff-0bc7e04e7e69533e27fda685be33f8f73aa05ba10cb84101d40fa88be716b951R555-R557

jaraco commented 2 years ago

For completeness, I encountered the error when trying to build pyoxidizer with PYO3_PYTHON=py:

``` pyoxidizer $ cargo build --release Compiling tempfile v3.3.0 Compiling duct v0.13.5 Compiling console v0.15.2 Compiling dirs v4.0.0 Compiling lzma-sys v0.1.19 Compiling bzip2-sys v0.1.11+1.0.8 Compiling libgit2-sys v0.14.0+1.5.0 Compiling libz-sys v1.1.8 Compiling zstd-sys v2.0.1+zstd.1.5.2 Compiling zstd-safe v5.0.2+zstd.1.5.2 Compiling pyo3-build-config v0.17.1 Compiling dirs-next v2.0.0 Compiling serde_derive v1.0.145 Compiling synstructure v0.12.6 Compiling futures-macro v0.3.24 Compiling tracing-attributes v0.1.22 Compiling thiserror-impl v1.0.37 Compiling webpki v0.22.0 Compiling sct v0.7.0 Compiling sct v0.6.1 error: failed to run custom build command for `pyo3-build-config v0.17.1` Caused by: process didn't exit successfully: `C:\Users\jaraco\code\fig-kokoro-dependencies\windows\pyoxidizer\target\release\build\pyo3-build-config-fd87f5bbfce9d065\build-script-build` (exit code: 1) --- stdout cargo:rerun-if-env-changed=PYO3_CONFIG_FILE cargo:rerun-if-env-changed=PYO3_NO_PYTHON cargo:rerun-if-env-changed=PYO3_PYTHON --- stderr Unable to create process using '"C:\Program Files\Python 3.11\py""': The system cannot find the file specified. error: Python script failed ```
jaraco commented 2 years ago

For what it's worth, on the same machine where Rust has trouble with the process, Python can launch it fine:

>>> subprocess.Popen(['py', '-V']).wait()
Python 3.11.0
0