-
Notifications
You must be signed in to change notification settings - Fork 793
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
pyo3 build or run failed (ld -lpython) when using a python manager like uv
or any venv
#4813
Comments
I wonder if we can fix this inside of pyo3-build-config somehow. I'd also like to add a CI job using |
I ran into issue when using #!/usr/bin/env bash
. .venv/bin/activate
LIBPYTHON_PATH="$(ldd "$(readlink "$(which python)")" | grep 'libpython' | sed 's|^\(.*\)/libpython.*|\1|' | xargs)"
PYTHON_HOME="$(dirname "$(dirname "$(readlink "$(which python)")")")"
cat <<EOF > build.rs
fn main() {
println!("cargo:rustc-env=LD_LIBRARY_PATH={}", "$LIBPYTHON_PATH");
println!("cargo:rustc-env=PYTHONHOME={}", "$PYTHON_HOME");
}
EOF
echo "build.rs has been generated successfully." out: fn main() {
println!("cargo:rustc-env=LD_LIBRARY_PATH={}", "/root/.local/share/uv/python/cpython-3.12.8-linux-x86_64-gnu/bin/../lib");
println!("cargo:rustc-env=PYTHONHOME={}", "/root/.local/share/uv/python/cpython-3.12.8-linux-x86_64-gnu");
} Without
And without
|
Experiencing the same issue. On macOS, it works when doing On Linux, it works when doing |
@astrojuanlu Are you using the latest version of uv? (and have you reinstalled the managed version with |
Indeed @zanieb , doing so made |
@Hennzau - I'm not able to reproduce this; the Python distribution installed by uv seems to work fine with pyo3. Can you provide instructions on how to reproduce this? (Or maybe something got fixed in either uv or pyo3 recently?)
See also my comment in astral-sh/uv#11006 (comment) . |
@geofft You have probably some global python3.12 installed somewhere with the according |
@geofft You can use this command to see where it's finding
|
This machine does not have a global Python 3.12 anywhere. (It's the NixOS live CD, which doesn't have a global anything.) Weirdly this is what I get:
(There is also no /etc/ld.so.cache file.) I also just tested the commands I provided on an Ubuntu 20.04 x86-64 machine and was unable to reproduce the issue either.
Can you please provide some more details on what the exact error is that you are getting on your machine and how I can reproduce the problem? From the linked uv issue - can I guess that your issue has to do with either a binary that links Python (as opposed to an extension module loaded by Python), or |
@geofft if I recall NixOs patches binaries since it does not use the standard file system structure. See https://github.com/nix-community/nix-ld . If I had to guess, the issue here is same as astral-sh/uv#11006 and involves running a binary that tries to link to python at runtime |
@geofft Hi again, something got fixed, probably, even if my
|
Hello,
I’ve faced quite a few challenges when building and running projects with PyO3 on systems that are fully managed by Python virtual environments. Most of the time, the issue boils down to errors like
ld -lpython3.X not found
. I wanted to share a workaround that worked for me.On UNIX systems, you can see the specific directories
ld
searches by running the following command:This will output something like:
However, when using tools like
uv
,anaconda
, etc., the Python installation is often standalone, andld
doesn’t know where to find the necessary libraries.For example, with
uv
, the Python library is typically located in~/.local/share/uv/python/cpython-{version}/lib
. To fix this, you can configureld
to include this directory in its search path. For Python 3.13.1 on x86_64 Linux, you can do the following:Then, reload the linker configuration using:
After that,
ld
should be able to find the Python libraries correctly.You can adapt this approach for any Python environment manager. I haven’t found clear documentation on this issue, so I hope this solution helps others who run into the same problem.
The text was updated successfully, but these errors were encountered: