inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

세상에서 제일 쉬운 러스트 프로그래밍

러스트로 파이썬 패키지 만들기 실습을 해보면 에러가 발생합니다

해결된 질문

212

seunghun.baek

작성한 질문수 1

1

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 를 찾을 수 없다는 에러가 발생합니다

python rust

답변 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)

Replit UI 변경으로 인한 실습 진행 문의

0

3

0

29강 5:00

0

10

1

설치 및 설정 가이드 노션 자료는 없나요 ?

0

16

2

실기시험 제출관련

0

123

2

6.20 작업형 2 과적합

0

135

3

install까지 설치 하였는데 start 가안됩니다.

1

21

1

8분54초 테이블 내용 문의

0

18

1

코딩팡 장업형2 베이스 라인 인코딩 종류 질문

0

38

2

multi_tool_agent.py 에서 arxiv tool 에러

0

16

1

로지스틱회귀, 회귀

0

46

2

가상 환경과 차이 도커 질문

0

24

1

자료 다운로드 위치 확인 부탁드립니다.

0

14

1

회귀 문제를 풀때 질문입니다.

0

52

1

불균형 처리 후 성능이 더 낮아졌다면,

0

60

2

실기 체험 제2유형 에러 문의

0

60

1

LIGHTGBM 으로 하면 pred값이 소수점 6자리까지 나오는게 맞나요

0

46

2

안녕하세요

1

29

3

3번문제 등분산 가정

0

46

2

작업형3 target 형 변환 질문

0

34

2

[작업형1] 연습문제 섹션1 ~ 10 의 section4

0

35

3

원핫인코딩과 레이블 인코딩에서 concat

0

56

2

agent 평가 관련 문의 드립니다.

0

19

1

가변 참조에 대한 주요 제약사항 퀴즈 문의

0

90

3

메모리 안전성에 대한 질문

0

78

1