Reputation: 564
I have the following Cargo.toml
file in the project root folder:
[package]
name = "python_rust_demo"
version = "0.1.0"
edition = "2021"
[dependencies]
pyo3 = { version = ">=0", features = ["abi3-py312", "extension-module"] }
pyo3-polars = { version = ">=0", features = [
"derive",
"dtype-array",
"dtype-struct",
] }
polars = { version = ">=0", features = [
"performant",
"lazy",
"diff",
"array_count",
"abs",
"cross_join",
"rank",
"ndarray",
"log",
"cum_agg",
"round_series",
"nightly",
"dtype-array",
"dtype-struct",
], default-features = false }
serde = { version = "*", features = ["derive"] }
ndarray = { version = "*" }
itertools = "*"
rand = "*"
rand_distr = "*"
Along with a pyproject.toml
file:
[project]
name = "python-rust-demo"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
"anytree",
"pyvis",
"pandas>=2",
"numpy>=2",
"statsmodels>=0",
"matplotlib>=3",
"seaborn>=0",
"plotly>=5",
"pyarrow>=18",
"polars>=1",
"scipy>=1",
"xgboost>=2",
"lightgbm>=4",
"scikit-learn>=1",
"numba>=0",
"num2words>=0",
"sqlalchemy>=2",
"pygount",
]
[dependency-groups]
dev = ["ipykernel>=6", "pytest>=8", "ruff>=0", "maturin>=1"]
[build-system]
requires = ["maturin>=1,<2"]
build-backend = "maturin"
I am currently using uv for my project dependency management:
uv venv
uv sync --all-extras
Then to build the python-rust code I will run:
maturin develop --uv --release
I have a single library crate with the src/lib.rs
module:
use pyo3::prelude::*;
/// Formats the sum of two numbers as string.
#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
Ok((a + b).to_string())
}
/// A Python module implemented in Rust. The name of this function must match
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
/// import the module.
#[pymodule]
fn string_sum(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
Ok(())
}
Everything runs fine in the terminal with or without activating the virtual env.
However the rust-analyzer keeps complaining about the pyo3 building:
Failed to run build scripts of some packages.
cargo check failed to start: Cargo watcher failed, the command produced no valid metadata (exit code: ExitStatus(ExitStatus(101))): Compiling pyo3-build-config v0.22.6 error: failed to run custom build command for pyo3-build-config v0.22.6 note: To improve backtraces for build dependencies, set the CARGO_PROFILE_DEV_BUILD_OVERRIDE_DEBUG=true environment variable to enable debug information generation.
Caused by: process didn't exit successfully: G:\tech_projects\python_rust_demo\target\debug\build\pyo3-build-config-c39c4a8839e7d80b\build-script-build (exit code: 1) --- stdout cargo:rerun-if-env-changed=PYO3_CONFIG_FILE cargo:rerun-if-env-changed=PYO3_NO_PYTHON cargo:rerun-if-env-changed=PYO3_ENVIRONMENT_SIGNATURE cargo:rerun-if-env-changed=PYO3_PYTHON cargo:rerun-if-env-changed=VIRTUAL_ENV cargo:rerun-if-env-changed=CONDA_PREFIX cargo:rerun-if-env-changed=PATH
--- stderr error: no Python 3.x interpreter found
Upvotes: 2
Views: 76