PyO3 / pyo3

Rust bindings for the Python interpreter
https://pyo3.rs
Other
11.5k stars 701 forks source link

Python shared object not found while using pyenv #1576

Open lainisourgod opened 3 years ago

lainisourgod commented 3 years ago

🐛 Python shared object not found while using pyenv

🌍 Environment

💥 Reproducing

main.rs

use pyo3::prelude::*;
use pyo3::types::IntoPyDict;

fn main() -> Result<(), ()> {
    Python::with_gil(|py| {
        main_(py).map_err(|e| {
            // We can't display Python exceptions via std::fmt::Display,
            // so print the error here manually.
            e.print_and_set_sys_last_vars(py);
        })
    })
}

fn main_(py: Python) -> PyResult<()> {
    let sys = py.import("sys")?;
    let version: String = sys.get("version")?.extract()?;
    let locals = [("os", py.import("os")?)].into_py_dict(py);
    let code = "os.getenv('USER') or os.getenv('USERNAME') or 'Unknown'";
    let user: String = py.eval(code, None, Some(&locals))?.extract()?;
    println!("Hello {}, I'm Python {}", user, version);
    Ok(())
}

cargo run

Result

error while loading shared libraries: libpython3.7m.so.1.0: cannot open shared object file: No such file or directory

Workaround

export LD_LIBRARY_PATH=~/.pyenv/versions/3.7.9/lib
davidhewitt commented 3 years ago

Thanks for reporting. It's expected that you will need to set LD_LIBRARY_PATH, however I agree the documentation could make this clearer. I'll try to add something.

Halkcyon commented 1 year ago

I've created this build.rs script to fix the problem for me:

fn main() {
    let home = std::env::var("HOME").expect("${HOME} is missing");
    println!("cargo:rustc-env=LD_LIBRARY_PATH={home}/.pyenv/versions/3.11.2/lib");
}

I found trying to use a cargo config with a home reference/variable wasn't resolving and I didn't want to hard-code the path.

Stargateur commented 3 weeks ago

I used https://github.com/pyenv/pyenv/issues/2647#issuecomment-1511754155, inside .bashrc or .zshrc:

for version in $(pyenv versions --bare)
do
        export LD_LIBRARY_PATH="$(pyenv root)/versions/$version/lib:$LD_LIBRARY_PATH"
done