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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

no function or associated item named with_gil found for struct pyo3::python::Python<'_> #1079

Closed
violet-dev opened this issue Aug 3, 2020 · 2 comments

Comments

@violet-dev
Copy link

馃悰 Bug Reports

When reporting a bug, please provide the following information. If this is not a bug report you can just discard this template.

馃實 Environment

  • Your operating system and version: Ubuntu 18.10
  • Your python version: Python 3.6.8 (default, Apr 9 2019, 04:59:38)
  • How did you install python (e.g. apt or pyenv)? Did you use a virtualenv?: apt
  • Your Rust version (rustc --version): rustc 1.45.0 (5c1f21c3b 2020-07-13)
  • Your PyO3 version: pyo3 = "0.11.1"
  • Have you tried using latest PyO3 master (replace version = "0.x.y" with git = "https://github.com/PyO3/pyo3")?:

馃挜 Reproducing

src/main.rs

use pyo3::prelude::*;

fn main() -> PyResult<()> {
    Python::with_gil(|py| {
        let builtins = PyModule::import(py, "builtins")?;
        let total: i32 = builtins.call1("sum", (vec![1, 2, 3],))?.extract()?;
        assert_eq!(total, 6);
        Ok(())
    })
}

Cargo.toml

[package]
name = "xxx"
version = "0.1.0"
authors = ["dc-koromo <koromo.software@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
pyo3 = "0.11.1"
xxx@xxx-B250M-DS3H:~/xxx$ cargo run --verbose
       Fresh unicode-xid v0.2.1
       Fresh proc-macro-hack v0.5.18
       Fresh smallvec v1.4.1
       Fresh unindent v0.1.6
       Fresh instant v0.1.6
       Fresh cfg-if v0.1.10
       Fresh scopeguard v1.1.0
       Fresh paste-impl v0.1.18
       Fresh lock_api v0.4.1
       Fresh proc-macro2 v1.0.19
       Fresh libc v0.2.74
       Fresh paste v0.1.18
       Fresh quote v1.0.7
       Fresh parking_lot_core v0.8.0
       Fresh syn v1.0.36
       Fresh parking_lot v0.11.0
       Fresh indoc-impl v0.3.6
       Fresh inventory-impl v0.1.7
       Fresh pyo3-derive-backend v0.11.1
       Fresh ctor v0.1.15
       Fresh ghost v0.1.2
       Fresh indoc v0.3.6
       Fresh pyo3cls v0.11.1
       Fresh inventory v0.1.7
       Fresh pyo3 v0.11.1
   Compiling xxx v0.1.0 (/home/xxx)
     Running `rustc --crate-name xxx --edition=2018 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -Cembed-bitcode=no -C debuginfo=2 -C metadata=d32b4e64e7579a4a -C extra-filename=-d32b4e64e7579a4a --out-dir /home/xxx/target/debug/deps -C incremental=/home/xxx/target/debug/incremental -L dependency=/home/xxx/target/debug/deps --extern pyo3=/home/xxx/target/debug/deps/libpyo3-8af27c426a1f6725.rlib -L native=/usr/lib`
error[E0599]: no function or associated item named `with_gil` found for struct `pyo3::python::Python<'_>` in the current scope
 --> src/main.rs:5:13
  |
5 |     Python::with_gil(|py| {
  |             ^^^^^^^^ function or associated item not found in `pyo3::python::Python<'_>`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0599`.
error: could not compile `xxx`.

Caused by:
  process didn't exit successfully: `rustc --crate-name xxx --edition=2018 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -Cembed-bitcode=no -C debuginfo=2 -C metadata=d32b4e64e7579a4a -C extra-filename=-d32b4e64e7579a4a --out-dir /home/xxx/target/debug/deps -C incremental=/home/xxx/target/debug/incremental -L dependency=/home/xxx/target/debug/deps --extern pyo3=/home/xxx/target/debug/deps/libpyo3-8af27c426a1f6725.rlib -L native=/usr/lib` (exit code: 1)

I tried running the example code, but it didn't build.

@rmarru
Copy link

rmarru commented Aug 3, 2020

It looks like the example you got from the github README is outdated, as Python::with_gil does not exist in the documentation for version 0.11.1.

You can find a simple and updated example in docs.rs:

use pyo3::prelude::*;
use pyo3::types::IntoPyDict;

fn main() -> PyResult<()> {
    let gil = Python::acquire_gil();
    let py = gil.python();
    let sys = py.import("sys")?;
    let version: String = sys.get("version")?.extract()?;

    let locals = [("os", py.import("os")?)].into_py_dict(py);
    let code = "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(())
}

@davidhewitt
Copy link
Member

Thanks for reporting.

Python::with_gil is a new API that will be the preferred way to use PyO3 once it releases in PyO3 0.12. The README is on the master branch where this API already exists, but perhaps I should have waited to update it until 0.12 is released.

As @rmarru notes, if you stick to the examples shipped with 0.11.1, everything works.

For now, I'm going to close unless others think I should backport README.md temporarily.

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

No branches or pull requests

3 participants