-
Notifications
You must be signed in to change notification settings - Fork 1k
docs: replace extension-module feature in docs
#5588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
7f2a8f2
033af5f
7e8882e
2b8e0c0
9add771
2e65a8f
bddb431
2b2cfde
da89296
e7826fb
e6891e6
23ac49a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,11 +73,11 @@ The PyO3 ecosystem has two packaging tools, [`maturin`] and [`setuptools-rust`], | |
|
|
||
| PyO3 has some Cargo features to configure projects for building Python extension modules: | ||
|
|
||
| - The `extension-module` feature, which must be enabled when building Python extension modules. | ||
| - The `PYO3_BUILD_EXTENSION_MODULE` environment variable, which must be set when building Python extension modules. | ||
| - The `abi3` feature and its version-specific `abi3-pyXY` companions, which are used to opt-in to the limited Python API in order to support multiple Python versions in a single wheel. | ||
|
|
||
| This section describes each of these packaging tools before describing how to build manually without them. | ||
| It then proceeds with an explanation of the `extension-module` feature. | ||
| It then proceeds with an explanation of the `PYO3_BUILD_EXTENSION_MODULE` environment variable. | ||
| Finally, there is a section describing PyO3's `abi3` features. | ||
|
|
||
| ### Packaging tools | ||
|
|
@@ -97,7 +97,7 @@ There are also [`maturin-starter`] and [`setuptools-rust-starter`] examples in t | |
|
|
||
| ### Manual builds | ||
|
|
||
| To build a PyO3-based Python extension manually, start by running `cargo build` as normal in a library project which uses PyO3's `extension-module` feature and has the [`cdylib` crate type](https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-crate-type-field). | ||
| To build a PyO3-based Python extension manually, start by running `cargo build` as normal in a library project with the [`cdylib` crate type](https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-crate-type-field) while the `PYO3_BUILD_EXTENSION_MODULE` environment variable is set. | ||
|
|
||
| Once built, symlink (or copy) and rename the shared library from Cargo's `target/` directory to your desired output directory: | ||
|
|
||
|
|
@@ -142,7 +142,7 @@ See [PEP 3149](https://peps.python.org/pep-3149/) for more background on platfor | |
|
|
||
| #### macOS | ||
|
|
||
| On macOS, because the `extension-module` feature disables linking to `libpython` ([see the next section](#the-extension-module-feature)), some additional linker arguments need to be set. `maturin` and `setuptools-rust` both pass these arguments for PyO3 automatically, but projects using manual builds will need to set these directly in order to support macOS. | ||
| On macOS, because the `PYO3_BUILD_EXTENSION_MODULE` environment variable disables linking to `libpython` ([see the next section](#the-extension-module-feature)), some additional linker arguments need to be set. `maturin` and `setuptools-rust` both pass these arguments for PyO3 automatically, but projects using manual builds will need to set these directly in order to support macOS. | ||
|
|
||
| The easiest way to set the correct linker arguments is to add a [`build.rs`](https://doc.rust-lang.org/cargo/reference/build-scripts.html) with the following content: | ||
|
|
||
|
|
@@ -193,17 +193,23 @@ For more discussion on and workarounds for MacOS linking problems [see this issu | |
|
|
||
| Finally, don't forget that on MacOS the `extension-module` feature will cause `cargo test` to fail without the `--no-default-features` flag (see [the FAQ](https://pyo3.rs/main/faq.html#i-cant-run-cargo-test-or-i-cant-build-in-a-cargo-workspace-im-having-linker-issues-like-symbol-not-found-or-undefined-reference-to-_pyexc_systemerror)). | ||
|
|
||
| ### The `extension-module` feature | ||
| ### The `PYO3_BUILD_EXTENSION_MODULE` environment variable | ||
|
|
||
| PyO3's `extension-module` feature is used to disable [linking](https://en.wikipedia.org/wiki/Linker_(computing)) to `libpython` on Unix targets. | ||
| <a name="the-extension-module-feature"></a> <!-- for backwards compatibility --> | ||
|
Comment on lines
+196
to
+198
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I want both the new heading referring to the environment variable, and the old one referring to the feature. |
||
|
|
||
| This is necessary because by default PyO3 links to `libpython`. | ||
| By default PyO3 links to `libpython`. | ||
| This makes binaries, tests, and examples "just work". | ||
| However, Python extensions on Unix must not link to libpython for [manylinux](https://www.python.org/dev/peps/pep-0513/) compliance. | ||
|
|
||
| The downside of not linking to `libpython` is that binaries, tests, and examples (which usually embed Python) will fail to build. | ||
| If you have an extension module as well as other outputs in a single project, you need to use optional Cargo features to disable the `extension-module` when you're not building the extension module. | ||
| See [the FAQ](faq.md#i-cant-run-cargo-test-or-i-cant-build-in-a-cargo-workspace-im-having-linker-issues-like-symbol-not-found-or-undefined-reference-to-_pyexc_systemerror) for an example workaround. | ||
| As a result, PyO3 uses an envionment variable `PYO3_BUILD_EXTENSION_MODULE` to disable linking to `libpython`. | ||
| This should only be set when building a library for distribution. | ||
| `maturin >= 1.9.4` and `setuptools-rust >= 1.12` will set this for you automatically. | ||
|
|
||
| > Note: historically PyO3 used an `extension-module` feature to perform the same function now done by the `PYO3_BUILD_EXTENSION_MODULE` env var. | ||
|
davidhewitt marked this conversation as resolved.
Outdated
|
||
| > This feature caused linking to be disabled for all compile targets, including Rust tests and benchmarks. | ||
| > | ||
| > Projects are encouraged to migrate off the feature, as it caused [major development pain](faq.md#i-cant-run-cargo-test-or-i-cant-build-in-a-cargo-workspace-im-having-linker-issues-like-symbol-not-found-or-undefined-reference-to-_pyexc_systemerror) due to the lack of linking. | ||
|
|
||
| ### `Py_LIMITED_API`/`abi3` | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The bullet doesn't match the intro :-)
It's probably worth saying that people don't generally need to set this themselves, because maturin/setuptools-rust set it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, done in 7e8882e and 2e65a8f