问题收录

Rust编译时链接的Python版本

Q: Hello, I’m running the example code in Calling Python in Rust code.I have all the rust things already, but after I ran cargo run.
It shows:

  1. dyld: Library not loaded: @rpath/libpython3.8.dylib
  2. Referenced from: /Users/slhmy/Workplace/python/python-rust/target/debug/python-rust
  3. Reason: image not found
  4. zsh: abort cargo run

I know I can fix it by export LD_LIBRARY_PATH, but actually thing makes me confused is why it choose python3.8 ?
I tried switching version by anacoda or using alias python=..., none of them will effect the version in rust code.
A: @slhmy pyO3 looks in your PATH to find python, you can control this using a virtualenv or you can override with PYO3_PYTHON env var

梳理

有关PYO3_PYTHON变量的用法,和PATH是不一样的。

PyO3 uses a build script to determine the Python version and set the correct linker arguments. By default it uses the python3 executable. You can override the Python interpreter by setting PYO3_PYTHON, e.g., PYO3_PYTHON=python3.6.

PATH里如果有多个python路径是按照顺序查找的,如果你想要某个python的路经被第一个查找到就应该使用

  1. export PATH={PYTHON_PATH}:$PATH

诸如这样的写法,这样它会被添加到PATH的最前端。