1

在遵循有关编译 Rust 库以进一步从 Python 导入的各种教程(我已经尝试过PyO3rust-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到相同的结果,其他一切都非常琐碎。

4

1 回答 1

1

python -m pytesttl/dr:使用而不是调用您的测试pytest


听起来您还没有安装 Python 扩展模块(例如,使用pip install),而是依赖于它位于您的工作目录中。这将允许您使用解释器导入它或main.py通过启动它python -m

$ tree .
.
├── bin
│   ├── __init__.py
│   └── sumlib.py
└── examples
    └── main.py

2 directories, 3 files
$ ipython --no-banner

In [1]: from bin import sumlib

In [2]: exit()

$ python -m examples.main
Successfully imported sumlib!

但不使用pytest examples/main.pyor python examples/main.py

$ pytest examples/main.py
======================================================= test session starts =======================================================
platform darwin -- Python 3.8.3, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: ...
plugins: typeguard-2.10.0
collected 0 items / 1 error

============================================================= ERRORS ==============================================================
________________________________________________ ERROR collecting examples/main.py ________________________________________________
ImportError while importing test module '.../examples/main.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
.../python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
examples/main.py:5: in <module>
    from bin import sumlib
E   ModuleNotFoundError: No module named 'bin'
...
$ python examples/main.py
Traceback (most recent call last):
  File "examples/main.py", line 5, in <module>
    from bin import sumlib
ModuleNotFoundError: No module named 'bin'

这是因为ipython两者python -m都将包含您的扩展模块的当前目录添加到 Python 的sys.path,pytest examples/main.pypython examples/main.py不添加。(请参阅pytest 文档。)您可以通过安装扩展模块或使用python -m pytest而不是运行测试来解决此问题pytest

于 2021-08-11T20:02:50.457 回答