-
Notifications
You must be signed in to change notification settings - Fork 944
Open
Labels
Description
Bug Description
Sample fails to compile for calling python in rust
Brand new to trying to run python in a rust app haven't found one sample that compiles yet.
Steps to Reproduce
- Cargo new python test.
- add py03 to Cargo.toml:
$ cat Cargo.toml
[package]
name = "pytest"
version = "0.1.0"
edition = "2021"
[dependencies]
[dependencies.pyo3]
version = "0.22.2"
features = ["auto-initialize"]
- Copy the sample source:
$ cat src/main.rs
use pyo3::ffi::c_str;
use pyo3::prelude::*;
use pyo3::types::IntoPyDict;
fn main() -> PyResult<()> {
Python::with_gil(|py| {
let sys = py.import("sys")?;
let version: String = sys.getattr("version")?.extract()?;
let locals = [("os", py.import("os")?)].into_py_dict(py);
let code = c_str!("os.getenv('USER') or os.getenv('USERNAME') or 'Unknown'");
let user: String = py.eval(code, None, Some(&locals))?.extract()?;
println!("Hello {}, I'm Python {}", user, version);
Ok(())
})
}
Backtrace
$ cargo build
Compiling once_cell v1.19.0
Compiling cfg-if v1.0.0
Compiling unindent v0.2.3
Compiling libc v0.2.158
Compiling memoffset v0.9.1
Compiling pyo3-build-config v0.22.2
Compiling pyo3-macros-backend v0.22.2
Compiling pyo3-ffi v0.22.2
Compiling pyo3 v0.22.2
Compiling pyo3-macros v0.22.2
Compiling pytest v0.1.0 (/home/haydon/workspace/py03-example/pytest)
warning: unused import: `pyo3::types::IntoPyDict`
--> src/main.rs:3:5
|
3 | use pyo3::types::IntoPyDict;
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
error[E0599]: no method named `import` found for struct `pyo3::Python` in the current scope
--> src/main.rs:7:22
|
7 | let sys = py.import("sys")?;
| ^^^^^^
|
help: there is a method `import_bound` with a similar name
|
7 | let sys = py.import_bound("sys")?;
| ~~~~~~~~~~~~
error[E0599]: no method named `import` found for struct `pyo3::Python` in the current scope
--> src/main.rs:10:33
|
10 | let locals = [("os", py.import("os")?)].into_py_dict(py);
| ^^^^^^
|
help: there is a method `import_bound` with a similar name
|
10 | let locals = [("os", py.import_bound("os")?)].into_py_dict(py);
| ~~~~~~~~~~~~
error[E0599]: no method named `eval` found for struct `pyo3::Python` in the current scope
--> src/main.rs:12:31
|
12 | let user: String = py.eval(code, None, Some(&locals))?.extract()?;
| ^^^^ method not found in `Python<'_>`
For more information about this error, try `rustc --explain E0599`.
warning: `pytest` (bin "pytest") generated 1 warning
error: could not compile `pytest` (bin "pytest") due to 3 previous errors; 1 warning emittedYour operating system and version
Kernel: Linux 6.10.9-arch1-1
Your Python version (python --version)
Python 3.12.5
Your Rust version (rustc --version)
rustc 1.81.0 (eeb90cda1 2024-09-04)
Your PyO3 version
0.22.2
How did you install python? Did you use a virtualenv?
system provided (via pacman).
Yes created a venv using python -m venv .
Additional Info
No response
Reactions are currently unavailable