I’m trying to run some Python code from a rust project and I can’t seem to get it to select the proper python interpreter.
My Cargo.toml:
[package]
name = "airfloe"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
# The name of the native library. This is the name which will be used in Python to import the
# library (i.e. `import string_sum`). If you change this, you must also change the name of the
# `#[pymodule]` in `src/lib.rs`.
name = "airfloe"
# "cdylib" is necessary to produce a shared library for Python to import from.
crate-type = ["cdylib"]
[[bin]]
name = "airfloe"
path = "src/main.rs"
[dependencies]
[dependencies.pyo3]
version = "0.20.0"
[features]
extension-module = ["pyo3/extension-module"]
default = ["pyo3/auto-initialize"]
My main.rs:
use pyo3::prelude::*;
fn main() -> PyResult<()> {
Python::with_gil(|py| {
py.run(r#"
import pkg_resources
installed_packages = pkg_resources.working_set
installed_packages_list = sorted(["%s==%s" % (i.key, i.version) for i in installed_packages])
for package in installed_packages_list:
print(package)
"#, None, None)?;
let sys = py.import("sys")?;
py.run(r#"
import sys
print(f"executable: {sys.executable}")
print(f"path: {sys.path}")
"#,
None, None)?;
Ok(())
})
}
Which prints:
pip==23.0.1
setuptools==65.5.0
executable: /Users/smartinez/src/rusty/airfloe/target/debug/airfloe
path: ['/Users/smartinez/.pyenv/versions/3.10.11/lib/python310.zip', '/Users/smartinez/.pyenv/versions/3.10.11/lib/python3.10', '/Users/smartinez/.pyenv/versions/3.10.11/lib/python3.10/lib-dynload', '/Users/smartinez/.pyenv/versions/3.10.11/lib/python3.10/site-packages']
So it’s clear it’s not using my activated pyenv environment and is instead showing the rust binary. I’d expect the executable to be something like /Users/smartinez/.pyenv/versions/3.10.11/bin/python
What am I missing?
>Solution :
What am I missing?
If you’re running python from rust, you’re in embedded mode, so the python runtime is loaded within rust, usually as a dll.
It may still load the packages from an existing environment (as path shows), but it will never use the python binary. If that’s what you want, run python as a subprocess.