-
|
In the "using Rust from Python" section from the PyO3 documentation it shows an example of using # "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"]I have been working on a Rust extension module which has (.env) $ cat Cargo.toml
[package]
name = "maturintest"
version = "0.1.0"
edition = "2024"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "maturintest"
[dependencies]
pyo3 = "0.28.2"
(.env) $ maturin develop
🔗 Found pyo3 bindings
🐍 Found CPython 3.11 at [...]
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.02s
📦 Built wheel for CPython 3.11 to [...]
✏️ Setting installed package as editable
🛠 Installed maturintest-0.1.0
(.env) $ python
>>> import maturintest
>>> maturintest.sum_as_string(3, 5)
'8'Which left me wondering:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Your result makes sense with current maturin, but I would still keep It is not that maturin never cares. The current build path can recover when the Cargo target is a normal Rust library: if no The manifest default is still useful because it makes the crate's intent explicit to Cargo and to tools that are not maturin. A plain For an extension module that also needs Rust tests, benches, examples, or downstream Rust imports, I would use: [lib]
crate-type = ["cdylib", "rlib"]That gives maturin the extension module artifact and keeps a Rust library artifact available for normal Rust tooling. I would only remove |
Beta Was this translation helpful? Give feedback.
Your result makes sense with current maturin, but I would still keep
cdylibin the manifest.It is not that maturin never cares. The current build path can recover when the Cargo target is a normal Rust library: if no
cdylibtarget is declared, maturin looks for a lib-like target and, on Rust 1.64 or newer, invokes Cargo with--crate-type cdylib. That explains whymaturin developstill worked in your test.The manifest default is still useful because it makes the crate's intent explicit to Cargo and to tools that are not maturin. A plain
cargo buildwill not otherwise produce the Python-loadable dynamic library artifact. Maturin's own packaging path also still expects to collect acdylib…