PyO3 / PyO3/pyo3

Running Python in Rust example in README errors

Open
#4,543 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

documentation
Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

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
  1. Cargo new python test.
  2. 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"]
  1. 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 emitted
Your 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Python-in-Rust sample in the README and compare its import and evaluation calls with the PyO3 0.22.2 APIs named in the compiler output. Reproduce the example with the issue's Cargo.toml, then update the README sample so cargo build completes without the reported errors or warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.