Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running Python in Rust example in README errors #4543

Open
haydonryan opened this issue Sep 10, 2024 · 4 comments
Open

Running Python in Rust example in README errors #4543

haydonryan opened this issue Sep 10, 2024 · 4 comments
Labels

Comments

@haydonryan
Copy link

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

@haydonryan haydonryan added the bug label Sep 10, 2024
@LilyFoote
Copy link
Contributor

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 LilyFoote reopened this Sep 10, 2024
@LilyFoote
Copy link
Contributor

Reopened because the README should be clearer about this.

@Mossman1215
Copy link

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 #4551

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

@haydonryan
Copy link
Author

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)

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

  2. 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants