Closed Antonio95 closed 6 months ago
This looks like an environment configuration issue. It seems like you're also sensing this.
Can you try running your compile with the environment variable PYO3_PRINT_CONFIG=1
? i.e. PYO3_PRINT_CONFIG=1 cargo test
. That will probably give us ideas what is going on.
Thank you for replying so quickly! You are absolutely right about my suspicions. I also think my python installation is probably not uncommon among users whose Mac came with a relatively recent version of MacOS, so I hope this will be of help to many people.
Here's the output of the PYO3_PRINT_CONFIG=1 cargo test
Compiling pyo3-ffi v0.20.3
error: failed to run custom build command for `pyo3-ffi v0.20.3`
Caused by:
process didn't exit successfully: `/Users/antonio/project/project/target/debug/build/pyo3-ffi-66d1fe44272a584f/build-script-build` (exit status: 101)
--- stdout
cargo:rerun-if-env-changed=PYO3_CROSS
cargo:rerun-if-env-changed=PYO3_CROSS_LIB_DIR
cargo:rerun-if-env-changed=PYO3_CROSS_PYTHON_VERSION
cargo:rerun-if-env-changed=PYO3_CROSS_PYTHON_IMPLEMENTATION
cargo:rerun-if-env-changed=PYO3_PRINT_CONFIG
-- PYO3_PRINT_CONFIG=1 is set, printing configuration and halting compile --
implementation=CPython
version=3.9
shared=true
abi3=false
lib_name=python3.9
lib_dir=/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib
executable=/Library/Developer/CommandLineTools/usr/bin/python3
pointer_width=64
build_flags=
suppress_build_script_link_lines=false
note: unset the PYO3_PRINT_CONFIG environment variable and retry to compile with the above config```
Strange. /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib
does exist on my machine.
Can you try installing Xcode from the macOS App Store, if you don't already have it? I assume that for whatever awful reason the macOS distribution of Python assumes that developers have Xcode installed.
Alternatively, just install Python 3.12 from python.org or homebrew and use that for more features and performance 😂
I've done as you proposed and installed Xcode. Now the path you mentioned in your last message also exists on my machine. However, I still get a build error:
dyld[12307]: Library not loaded: @rpath/Python3.framework/Versions/3.9/Python3
Referenced from: <85811BC8-D961-30BB-BC9B-38AD5AD9768A> /Users/antonio/project/project/target/debug/deps/common-a56ca8383ddf5208
Reason: no LC_RPATH's found
error: test failed, to rerun pass `-p hcs-common --lib`
Caused by:
process didn't exit successfully: `/Users/antonio/project/project/target/debug/deps/common-a56ca8383ddf5208` (signal: 6, SIGABRT: process abort signal)
When setting PYO3_PRINT_CONFIG=1
, I get:
error: failed to run custom build command for `pyo3-ffi v0.20.3`
Caused by:
process didn't exit successfully: `/Users/antonio/project/project/target/debug/build/pyo3-ffi-66d1fe44272a584f/build-script-build` (exit status: 101)
--- stdout
cargo:rerun-if-env-changed=PYO3_CROSS
cargo:rerun-if-env-changed=PYO3_CROSS_LIB_DIR
cargo:rerun-if-env-changed=PYO3_CROSS_PYTHON_VERSION
cargo:rerun-if-env-changed=PYO3_CROSS_PYTHON_IMPLEMENTATION
cargo:rerun-if-env-changed=PYO3_PRINT_CONFIG
-- PYO3_PRINT_CONFIG=1 is set, printing configuration and halting compile --
implementation=CPython
version=3.9
shared=true
abi3=false
lib_name=python3.9
lib_dir=/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib
executable=/Applications/Xcode.app/Contents/Developer/usr/bin/python3
pointer_width=64
build_flags=
suppress_build_script_link_lines=false
note: unset the PYO3_PRINT_CONFIG environment variable and retry to compile with the above config
For the record, I still have the .cargo/config
file with the same contents I mentioned on the OP.
See the section about rpath
in the bottom of the macOS building entry: https://pyo3.rs/v0.21.0/building-and-distribution.html?highlight=rpath#macos
That finally fixed execution. Thank you so much!
Glad to hear!
Bug Description
I have a small Rust project that relies on
pyo3
to run some python code of my own with methods such asPython::with_gil
. The project compiles and runs on Ubuntu, but executingcargo test
on a Mac results in a long error message. I have gone through the solutions proposed in other threads, for instance running the command with the recommended compilation flags and adding the following to.cargo/config
(from the project root):The Mac has OS version 14.4 and an M2 chip.
I only have Python3 installed.
which python3
outputs/usr/bin/python3
. Nopython
command is available.P.S.: Thanks a lot for this very useful crate and the support you have provided other people with similar compilation issues.
Steps to Reproduce
pyo3
functionalitycargo test
Backtrace
Your operating system and version
MacOS Sonoma 14.4
Your Python version (
python --version
)Python 3.9.6 (as mentioned, this is
python3 --version
: nopython
command on my system)Your Rust version (
rustc --version
)rustc 1.77.0-nightly (I have
rust-toolchain
set to nightly)Your PyO3 version
0.20.3
How did you install python? Did you use a virtualenv?
As part of the command line developer tools.
More context: in the last versions of MacOS, python doesn't come installed by default, but there is a stub at
usr/bin/python3
. Upon trying to run it for the first time, one is prompted to install the dev tools, which results in the python3 binary at that path. More info here.N.B.: Despite what the link says, installing the dev tools doesn't result in python2 being installed as a binary in
/usr/bin/python
in newer version os MacOS (like mine).Additional Info
It seems (I might be misjudging things, as I am not very familiar with the issue) that the linker is looking for python in
/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib
. But I don't even have an/Applications/Xcode.app
folder. I have python binaries at/usr/bin/python3
and at/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/bin/python3.9
.