PyO3 / PyO3/pyo3

`PYO3_PYTHON` and `LD_LIBRARY_PATH` pointing to `venv` are ignored

Open
#4,841 9 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

Bug Description

I would like pyo3 to use the .venv I've created in my python library, when calling functions from that library from rust. - but it ignores the values configured in my environment.

From what I've read I understood it as PYO3_PYTHON and/or LD_LIBRARY_PATH can be used to configure the path to the python interpreter executable. I exported them in the shell and then ran my rust project like so

export PYO3_PYTHON=/path/to/my/python/project/.venv/bin/python
export LD_LIBRARY_PATH=/path/to/my/python/project/.venv/bin/python
cargo run --bin fxctl ping

ping is just a function I use to test the setup. In the rust app, the ping function is invoked through a clap command. The handler looks like so:

use pyo3::prelude::*;

pub fn ping() -> PyResult<()> {
    Python::with_gil(|py| {
        let sys = PyModule::import(py, "sys").unwrap();

        let py_path: String = sys.getattr("executable").unwrap().extract().unwrap();
        let py_version: String = sys.getattr("version").unwrap().extract().unwrap();

        println!("path: {}\nversion: {}", py_path, py_version);
    });
    Ok(())
}
Steps to Reproduce

Reproduction repository:
https://github.com/jakob-lilliemarck/pyo3-python-env-ignored

Backtrace
There is no backtrace as there is no actual error
Your operating system and version

Linux 6.6.65-1-MANJARO #1 SMP PREEMPT_DYNAMIC Wed Dec 11 22:24:04 UTC 2024 x86_64 GNU/Linux

Your Python version (python --version)

3.12.7 (main, Oct 1 2024, 11:15:50) [GCC 14.2.1 20240910]

Your Rust version (rustc --version)

rustc 1.85.0-nightly (a224f3807 2024-12-09)

Your PyO3 version

0.23.3

How did you install python? Did you use a virtualenv?

Can't recall, but from the looks of it I installed the global one using brew, then I created a virtual env in my project by python -m venv .venv

Additional Info

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked reproduction repository and inspect how PyO3 0.23.3 discovers and configures the Python interpreter and library paths during the Cargo build and runtime. Reproduce the reported PYO3_PYTHON and LD_LIBRARY_PATH environment, then compare the selected executable and version with the expected .venv; done means the behavior is corrected or clearly documented with a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
backend, build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.