PyO3 / pyo3

Rust bindings for the Python interpreter
https://pyo3.rs
Apache License 2.0
12.45k stars 768 forks source link

Running Python in Rust example in README errors #4543

Open haydonryan opened 2 months ago

haydonryan commented 2 months ago

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"]

3. 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

```shell
$ 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

LilyFoote commented 2 months ago

The problem here is that you're using the docs for the as-yet-unreleased 0.23 version, but the 0.22.2 version for your code.

LilyFoote commented 2 months ago

Reopened because the README should be clearer about this.

Mossman1215 commented 2 months ago

hello, I have also experienced this issue and fixed the example based on suggestions from rust-analyser

it seems like there's been _bound added to some of the functions that are called in the readme

I have put the version that worked on my demo project into this pull request https://github.com/PyO3/pyo3/pull/4551

I am a novice with rust and like the concept of the library.

haydonryan commented 3 weeks ago

I was able to run the sample from the latest tag and everything ran great - thank you! This is a problem I've seen before, and feel like there's a few options you could look to improve the issue.

  1. Add notes to both the sample applications (rust from python, python from rust). I would put this as the first step:

    [!IMPORTANT]
    Active development is done in the main branch. Breaking changes will and have occurred. Before attempting these instructions go to the latest tagged release. here. Do not use the branches named release-vxxxx.
    (I ran into that problem)

  2. Develop in a develop branch, and leave main as the latest stable release. (Then you have the problem of protecting pushes to main)

  3. Another option would be to have people clone the repo and check out the latest tag as step one, but as dependencies aren't needed from GitHub probably wouldn't be a great option.

Thanks again for all your work here!