在遵循有关编译 Rust 库以进一步从 Python 导入的各种教程(我已经尝试过PyO3和rust-cpython)时,我已经能够构建一个简单的库并成功地从我的main.py
交互式 Python shell 中导入它。
但是,在尝试测试纯 Python 和我的 Rust 库并对其进行基准测试时,我总是遇到导入错误:
_____________________________________________ ERROR collecting main.py _____________________________________________
ImportError while importing test module '/Users/xxx/pyo3/sumlib/main.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/Users/xxx/.pyenv/versions/3.8.2/lib/python3.8/importlib/__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
main.py:1: in <module>
import sumlib
E ModuleNotFoundError: No module named 'sumlib'
同样,该库从主 python 文件和交互式 shell(如IPython
.
我在 macOS Big Sur 上,Python 3.8 使用pyenv
. 这是我的Cargo.toml
:
[package]
name = "sumlib"
version = "0.1.0"
edition = "2018"
[lib]
name = "sumlib"
# "cdylib" is necessary to produce a shared library for Python to import from.
#
# Downstream Rust code (including code in `bin/`, `examples/`, and `tests/`) will not be able
# to `use string_sum;` unless the "rlib" or "lib" crate type is also included, e.g.:
# crate-type = ["cdylib", "rlib"]
crate-type = ["cdylib", "rlib"]
[dependencies.pyo3]
version = "0.14.2"
features = ["extension-module"]
PyO3
从andrust-cpython
到相同的结果,其他一切都非常琐碎。