candle icon indicating copy to clipboard operation
candle copied to clipboard

UV is incompatible with pyo3 for the reinforcement-learning example

Open greenrazer opened this issue 8 months ago • 0 comments

If you create a venv using uv and pip install gymnasium, the example command (cargo run --example reinforcement-learning --features=pyo3 --package candle-examples -- pg) will not find any of your installed libraries.

There's a discussion thread about workarounds here, but the solution of

$ find ~/.local/share/uv/python -name "libpython3.xx.dylib"
> /path/to/pythondir/lib/libpython3.xx.dylib
DYLD_FALLBACK_LIBRARY_PATH=/path/to/pythondir/lib/ CARGO_BUILD_RUSTFLAGS="-L /path/to/pythondir/lib/" cargo run --example reinforcement-learning --features=pyo3 --package candle-examples -- pg

Gives an error

Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00000001f8918840 (most recent call first):
  <no Python frame>

Which is failing because it is searching for python libs in \install which doesn't exist.

My solution (Mac OS)

This solution is pretty brittle, but it works. It renames the install names of the python libs to point at itself instead of the /install directory.

$ uv venv
$ source .venv/bin/activate
$ uv pip install gymnasium
$ which python3.xx 
> /path/to/.venv/bin/python3.xx
$ find ~/.local/share/uv/python -name "libpython3.xx.dylib"
> /path/to/pythondir/lib/libpython3.xx.dylib
$ cp /path/to/pythondir/lib/libpython3.xx.dylib ~/libpython3.xx.dylib.backup
$ install_name_tool -id "/path/to/pythondir/lib/libpython3.xx.dylib" "/path/to/pythondir/lib/libpython3.xx.dylib"
$ otool -L /path/to/pythondir/lib/libpython3.xx.dylib
$ cargo clean
$ export PYTHONHOME=/path/to/pythondir
$ export PYTHONPATH=/path/to/pythondir/lib/python3.xx
$ cargo run --example reinforcement-learning --features=pyo3 --package candle-examples -- pg

System Info: cargo 1.85.1 (d73d2caf9 2024-12-31) rustc 1.85.1 (4eb161250 2025-03-15) Apple M3 Max (36 GB) MacOS 15.3.2 python 3.12.7

greenrazer avatar Mar 28 '25 21:03 greenrazer