Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python shared object not found while using pyenv #1576

Open
lainisourgod opened this issue Apr 26, 2021 · 3 comments
Open

Python shared object not found while using pyenv #1576

lainisourgod opened this issue Apr 26, 2021 · 3 comments

Comments

@lainisourgod
Copy link

🐛 Python shared object not found while using pyenv

🌍 Environment

  • Your operating system and version: Windows 10 20h2 + WSL2
  • Your python version: 3.7.9
  • How did you install python (e.g. apt or pyenv)? Did you use a virtualenv?: env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.7.9
  • Your Rust version (rustc --version): rustc 1.51.0 (2fd73fabe 2021-03-23)
  • Your PyO3 version: *0.13.2
  • Have you tried using latest PyO3 main (replace version = "0.x.y" with git = "https://github.com/PyO3/pyo3")?: yes

💥 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
Copy link
Member

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
Copy link

Halkcyon commented Mar 12, 2023

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
Copy link

Stargateur commented Jun 17, 2024

I used pyenv/pyenv#2647 (comment), inside .bashrc or .zshrc:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants