0

Very new to Rust.

I'm trying to build a python module in Rust using rust-cypthon. Currently, I can access cpython types but can't call py_module_initializer! which I believe is required to make the module run in python.

When compiling the code, changing the extension to .so and putting the file in with my python scripts and importing, I get this error:

ImportError: dynamic module does not define init function (initpyext_rust_onboard)

Cargo.toml

[lib]
crate-type = ["cdylib"]

[dependencies]
csv = "1.1.1"
serde = "1.0.99"
serde_derive="1.0"
serde_json= "1.0"
serde_yaml = "0.7.1"

[dependencies.cpython]
version = '0.3.0'
default-features = false
features = ["python27-sys", "extension-module-2-7"]

Dependencies imported to lib.rs

#[macro_use]
extern crate serde;
extern crate serde_derive;

#[macro_use]extern crate cpython;


use std::fs::File;
use std::collections::HashMap;
use std::borrow::Cow;
use std::error::Error;
use cpython::{Python, PyObject, PyResult, PyString, PyFloat};
use csv::DeserializeRecordsIter;

2 Answers 2

2

When compiling the code, changing the extension to .so and putting the file in with my python scripts

What are you renaming it from? The rust-cpython documentation says

On Mac OS, you will need to rename the output from *.dylib to *.so. On Windows, you will need to rename the output from *.dll to *.pyd.

and on Linux you shouldn't be renaming anything.

The problem is probably that you are not actually building a shared object. You have

[lib]
crate-type = ["dylib"]

but the rust-cpython readme says

[lib]
crate-type = ["cdylib"]

Mind the c! cdylib and dylib are different things in rust.

Sign up to request clarification or add additional context in comments.

2 Comments

I've changed it from a dylib file to a .so file as am on Mac. I've changed dylib to cdylib and changed features to features = ["python27-sys", "extension-module-2-7"] - now it will compile but I still can't access py_module_initialiser!
Edit: Realised I have access to the macro now but my IDE was ignoring it. Cheers for the solution!
1

According to the cpython homepage, you need to enable the extension-module module feature, so try with:

[dependencies.cpython]
version = '0.3.0'
default-features = false
features = ["python27-sys", "extension-module-2-7"]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.