러스트로 파이썬 패키지 만들기 실습을 해보면 에러가 발생합니다
pipenv shell 환경에서 maturin develop 을 하면 아래와 같이 오류가 뜹니다
(pyo3-9vp6XEz5) D:\D\Work\study\Rust\inflean\easy-rust\pyo3>maturin develop
🔗 Found pyo3 bindings
🐍 Found CPython 3.12 at C:\Users\INNO-A-1328\.virtualenvs\pyo3-9vp6XEz5\Scripts\python.exe
📡 Using build options features from pyproject.toml
Compiling once_cell v1.20.2
Compiling unindent v0.2.3
Compiling cfg-if v1.0.0
Compiling libc v0.2.169
Compiling pyo3-build-config v0.23.3
Compiling memoffset v0.9.1
Compiling pyo3-ffi v0.23.3
Compiling pyo3-macros-backend v0.23.3
Compiling pyo3 v0.23.3
Compiling pyo3-macros v0.23.3
Compiling fibonacci v0.1.0 (D:\D\Work\study\Rust\inflean\easy-rust\pyo3)
error[E0599]: no method named add_function found for reference &pyo3::types::PyModule in the current scope
--> src\lib.rs:17:7
|
17 | m.add_function(wrap_pyfunction!(run, m)?)?;
| ^^^^^^^^^^^^ method not found in &PyModule
error[E0277]: the trait bound &pyo3::types::PyModule: WrapPyFunctionArg<'_, _> is not satisfied
--> src\lib.rs:17:42
|
17 | m.add_function(wrap_pyfunction!(run, m)?)?;
| ----------------------^-
| | |
| | the trait WrapPyFunctionArg<'_, _> is not implemented for &pyo3::types::PyModule
| required by a bound introduced by this call
|
= help: the following other types implement trait WrapPyFunctionArg<'py, T>:
&pyo3::Borrowed<'_, 'py, pyo3::types::PyModule>
&pyo3::Bound<'py, pyo3::types::PyModule>
pyo3::Borrowed<'_, 'py, pyo3::types::PyModule>
pyo3::Bound<'py, pyo3::types::PyModule>
pyo3::Python<'py>
error[E0277]: the trait bound &pyo3::types::PyModule: From<BoundRef<'_, '_, pyo3::types::PyModule>> is not satisfied
--> src\lib.rs:15:1
|
15 | #[pymodule]
| ^^^^^^^^^^^ the trait From<BoundRef<'_, '_, pyo3::types::PyModule>> is not implemented for &pyo3::types::PyModule, which is required by BoundRef<'_, '_, pyo3::types::PyModule>: Into<_>
|
= note: required for BoundRef<'_, '_, pyo3::types::PyModule> to implement Into<&pyo3::types::PyModule>
= note: this error originates in the attribute macro pymodule (in Nightly builds, run with -Z macro-backtrace for more info)
Some errors have detailed explanations: E0277, E0599.
For more information about an error, try rustc --explain E0277.
error: could not compile fibonacci (lib) due to 3 previous errors
💥 maturin failed
#[pymodule] 이하 부분을 주석처리하면 아래 에러는 해결되나 python main.py 실행하면 fibonacci 를 찾을 수 없다는 에러가 발생합니다
Câu trả lời 1
0
안녕하세요, pyo3가 버전이 올라가면서 생긴 문제입니다. 먼저 lib.rs는 다음과 같이 수정하세요.
use pyo3::prelude::*;
fn _run(n: u64) -> u64 {
match n {
0 => 0,
1 => 1,
_ => _run(n - 1) + _run(n - 2),
}
}
#[pyfunction]
fn run(n: u64) -> PyResult<u64> {
Ok(_run(n))
}
/// A Python module implemented in Rust.
#[pymodule]
fn fibonacci(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(run, m)?)?;
Ok(())
}그리고 Cargo.toml 은 다음과 같습니다(피처만 추가하셔도 됩니다)
[package]
name = "fibonacci"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "fibonacci"
crate-type = ["cdylib"]
[dependencies.pyo3]
version = "0.23.3"
features = ["extension-module"]이제 패키지가 잘 빌드되고 파이썬에서도 함수 호출이 정상적으로 기능합니다.
Python 3.12.6 (main, Sep 15 2024, 11:07:06) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import fibonacci
>>> fibonacci.run(3)
2
>>> fibonacci.run(3000)
강의 업데이트
0
4
1
블로그 내용 정리
0
4
0
3.1.강화학습 기본 알고리즘-마르코프 결정과정
0
10
2
매수 실행 시점 관련
0
9
0
영상 다운로드는 안되나요?
0
15
1
수강자료 다운로드 관련 문의
0
23
2
Opus/Sonnet 버전 관련 문의
1
22
2
"run_nvidia_gpu.bat" 실행후
0
19
1
섹션2 질문이요
1
25
2
섹션 2
0
19
1
섹션1.9 질문입니다!
1
37
3
웹서비스 방법
0
34
2
MCP 정의가 잘못되어 있음 (Chapter2)
0
33
1
수강 연장 문의드립니다.
0
34
2
13. (App 2) 기본기 훈련 에서
0
35
1
채점 프로그램 미작동
0
29
2
챌린지 시작일 문의
0
40
0
2. 어떤 도구를 사용하는 것이 가장 유리할까? 강의 중
0
32
1
안녕하세요 ppt 자료 메일로 부탁드립니다
0
24
1
11차시 Antigravity IDE 설치 후
0
34
1
링크드 리스트 중간 삽입삭제 시간복잡도 질문
0
37
2
Kaggle 노트북(predict.ipynb) 관련
1
39
2
가변 참조에 대한 주요 제약사항 퀴즈 문의
0
113
3
메모리 안전성에 대한 질문
0
92
1

