Can't set Python version when running rust-analyzer and PyO3 on WSL

I am trying to tell PyO3 to use Python 3.11 by setting the environment variable PYTHON_SYS_EXECUTABLE, but Python 3.6 continues to be used, which is too old a version for PYO3, and so proc-macros fail. I am running on a Windows 11 machine and using a WSL RedHat 8 distro. I've tried ensuring that VS Code inherits the environment variable, and it does seem to, but still rust analyzer crashes and tells me :
error: failed to run custom build command for `pyo3-ffi v0.23.5`
note: To improve backtraces for build dependencies, set the CARGO_PROFILE_DEV_BUILD_OVERRIDE_DEBUG=true environment variable to enable debug information generation.
Caused by:
process didn't exit successfully: `/tools/rust/target/debug/build/pyo3-ffi-0d4cba8584d692ee/build-script-build` (exit status: 1)
--- stdout
cargo:rustc-check-cfg=cfg(Py_LIMITED_API)
cargo:rustc-check-cfg=cfg(Py_GIL_DISABLED)
cargo:rustc-check-cfg=cfg(PyPy)
cargo:rustc-check-cfg=cfg(GraalPy)
cargo:rustc-check-cfg=cfg(py_sys_config, values("Py_DEBUG", "Py_REF_DEBUG", "Py_TRACE_REFS", "COUNT_ALLOCS"))
cargo:rustc-check-cfg=cfg(invalid_from_utf8_lint)
cargo:rustc-check-cfg=cfg(pyo3_disable_reference_pool)
cargo:rustc-check-cfg=cfg(pyo3_leak_on_drop_without_reference_pool)
cargo:rustc-check-cfg=cfg(diagnostic_namespace)
cargo:rustc-check-cfg=cfg(c_str_lit)
cargo:rustc-check-cfg=cfg(rustc_has_once_lock)
cargo:rustc-check-cfg=cfg(fn_ptr_eq)
cargo:rustc-check-cfg=cfg(Py_3_7)
cargo:rustc-check-cfg=cfg(Py_3_8)
cargo:rustc-check-cfg=cfg(Py_3_9)
cargo:rustc-check-cfg=cfg(Py_3_10)
cargo:rustc-check-cfg=cfg(Py_3_11)
cargo:rustc-check-cfg=cfg(Py_3_12)
cargo:rustc-check-cfg=cfg(Py_3_13)
cargo:rerun-if-env-changed=PYO3_CROSS
cargo:rerun-if-env-changed=PYO3_CROSS_LIB_DIR
cargo:rerun-if-env-changed=PYO3_CROSS_PYTHON_VERSION
cargo:rerun-if-env-changed=PYO3_CROSS_PYTHON_IMPLEMENTATION
cargo:rerun-if-env-changed=PYO3_PRINT_CONFIG
--- stderr
error: the configured Python interpreter version (3.6) is lower than PyO3's minimum supported version (3.7)
Answer
When you're using PyO3 with Rust and rust-analyzer on WSL, it can be super picky about which Python version it uses — especially if you’ve got multiple versions or virtual environments floating around.
Here’s what worked for me:
Tell PyO3 which Python to use
Set thePYO3_PYTHON
environment variable to the exact Python you want (like your virtualenv one):export PYO3_PYTHON=/home/youruser/.venv/bin/python
You can stick that in your
.bashrc
or.zshrc
so you don’t have to keep typing it.Make sure rust-analyzer picks it up
If you’re using VS Code, go into yoursettings.json
and add this:"rust-analyzer.cargo.extraEnv": { "PYO3_PYTHON": "/home/youruser/.venv/bin/python" }
That way rust-analyzer knows what’s up when it does its thing.
Double-check your Python in WSL
Runwhich python
andpython --version
just to be sure the Python in your terminal is the same one PyO3 is using. If they don’t match, that can cause all kinds of weirdness.Restart stuff
Not kidding — restarting VS Code and even your WSL session can help. Sometimes things don’t update until you do.Hope that helps!
Enjoyed this question?
Check out more content on our blog or follow us on social media.
Browse more questions