Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Selecting the right pyenv virtual environment from Rust with PyO3

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading