Repository navigation
Replies: 3 comments 3 replies
|
fwiw, I tested this with Without So for tests I’d either enable use pyo3::prelude::*;
use std::sync::OnceLock;
fn with_python<F, R>(f: F) -> R
where
F: for<'py> FnOnce(Python<'py>) -> R,
{
static INIT: OnceLock<()> = OnceLock::new();
INIT.get_or_init(Python::initialize);
Python::attach(f)
}Then call The “shared between tests” bit is process-scoped: unit tests in the same Rust test binary share the initialized interpreter, while separate integration test binaries are separate processes, so each binary should initialize before touching Python too. I don’t see a test-specific docs page, mostly the |
|
Another small obstacle I'm encountering is, how do I let Cargo/Pyo3 know what interpreter to use so that my Python module is detected and can be imported while running my Rust test suite. I figured I could try |
|
For the follow-up about
First inspect the Python selected by your project: uv run python -c 'import sys, sysconfig; print(sys.executable); print(sysconfig.get_config_var("LIBDIR")); print(sysconfig.get_config_var("LDLIBRARY"))'If the reported library directory contains the matching shared library, try this per-command Linux configuration: test_python="$(uv run python -c 'import sys; print(sys.executable)')"
test_libdir="$("$test_python" -c 'import sysconfig; print(sysconfig.get_config_var("LIBDIR"))')"
PYO3_PYTHON="$test_python" \
LD_LIBRARY_PATH="$test_libdir${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" \
uv run cargo testIf the library is absent, fix the selected Python installation first. Pointing the loader at a different Python version's library is not a substitute. After the executable starts, any remaining Reference: PyO3 — dynamically embedding Python. |
Uh oh!
There was an error while loading. Please reload this page.
My Rust tests fail with:
Is there documentation on how to write Rust tests that indirectly call
Python::attach()? Should I initialize Python in each test that directly or indirectly uses it? Will the interpreter be shared between each test, or will each test use a different interpreter?All reactions