首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Rust中的pyo3构建python库时出错“root中没有py”

使用Rust中的pyo3构建python库时出错“root中没有py”
EN

Stack Overflow用户
提问于 2019-09-14 14:52:30
回答 1查看 280关注 0票数 2

我的环境

  • Ubuntu 18.04
  • 生锈1.39.0-每晚
  • Python 3.7.3

我想做什么和问题

我想为python写一个库。作为我的惯例,我尝试了文档中的代码。

Cargo.toml

代码语言:javascript
复制
[package]
name = "example"
version = "0.1.0"
authors = ["Yudai Hayashi"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "example"
crate-type = ["cdylib"]

[dependencies]

[dependencies.pyo3]
version = "*"
features = ["extension-module"]

src/lib.rs

代码语言:javascript
复制
#![feature(proc_macro, specialization)]

extern crate pyo3;
use pyo3::{py, PyResult, Python, PyModule};

use pyo3::py::modinit as pymodinit;

// add bindings to the generated python module
// N.B: names: "librust2py" must be the name of the `.so` or `.pyd` file
/// This module is implemented in Rust.
#[pymodinit(rust2py)]
fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {

    #[pyfn(m, "sum_as_string")]
    // pyo3 aware function. All of our python interface could be declared in a separate module.
    // Note that the `#[pyfn()]` annotation automatically converts the arguments from
    // Python objects to Rust values; and the Rust return value back into a Python object.
    fn sum_as_string_py(_: Python, a:i64, b:i64) -> PyResult<String> {
       let out = sum_as_string(a, b);
       Ok(out)
    }

    Ok(())
}

// logic implemented as a normal Rust function
fn sum_as_string(a:i64, b:i64) -> String {
    format!("{}", a + b).to_string()
}

当我构建这个程序时,我遇到了一些错误。

代码语言:javascript
复制
error[E0432]: unresolved import `pyo3::py`
 --> src/lib.rs:4:12
  |
4 | use pyo3::{py, PyResult, Python, PyModule};
  |            ^^
  |            |
  |            no `py` in the root
  |            help: a similar name exists in the module: `Py`

error[E0432]: unresolved import `pyo3::py`
 --> src/lib.rs:6:11
  |
6 | use pyo3::py::modinit as pymodinit;
  |           ^^ could not find `py` in `pyo3`

error: cannot determine resolution for the attribute macro `pymodinit`
  --> src/lib.rs:11:3
   |
11 | #[pymodinit(rust2py)]
   |   ^^^^^^^^^
   |
   = note: import resolution is stuck, try simplifying macro imports

error: cannot find attribute macro `pyfn` in this scope
  --> src/lib.rs:14:7
   |
14 |     #[pyfn(m, "sum_as_string")]
   |       ^^^^

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0432`.
error: Could not compile `example`.

此错误消息表明pyo3模块中没有"py“。我查找此错误消息,但找不到类似的错误。

如何处理这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-14 15:08:42

在最新版本的py中有pyo3。您已经了解了与库的旧版本相关的文档或示例。使用此mod的最后一个版本是0.2.7。

如果您除了运行代码之外没有其他兴趣,请将Cargo.toml依赖项中所需的Cargo.toml版本设置为0.2.7,并且应该立即编译(粗略地看一看,该版本中似乎存在所有符号)。

如果您想实际研究较新的版本,新文件有最新的示例。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57936617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档