|
| 1 | +# Features Reference |
| 2 | + |
| 3 | +PyO3 provides a number of Cargo features to customise functionality. This chapter of the guide provides detail on each of them. |
| 4 | + |
| 5 | +By default, the `macros` and `auto-initialize` features are enabled. |
| 6 | + |
| 7 | +## Features for extension module authors |
| 8 | + |
| 9 | +### `extension-module` |
| 10 | + |
| 11 | +This feature is required when building a Python extension module using PyO3. |
| 12 | + |
| 13 | +It tells PyO3's build script to skip linking against `libpython.so` on Unix platforms, where this must not be done. |
| 14 | + |
| 15 | +See the [building and distribution](building_and_distribution.md#linking) section for further detail. |
| 16 | + |
| 17 | +### `abi3` |
| 18 | + |
| 19 | +This feature is used when building Python extension modules to create wheels which are compatible with multiple Python versions. |
| 20 | + |
| 21 | +It restricts PyO3's API to a subset of the full Python API which is guaranteed by [PEP 384](https://www.python.org/dev/peps/pep-0384/) to be forwards-compatible with future Python versions. |
| 22 | + |
| 23 | +See the [building and distribution](building_and_distribution.md#py_limited_apiabi3) section for further detail. |
| 24 | + |
| 25 | +### `abi3-py36` / `abi3-py37` / `abi3-py38` / `abi3-py39` |
| 26 | + |
| 27 | +These features are an extension of the `abi3` feature to specify the exact minimum Python version which the multiple-version-wheel will support. |
| 28 | + |
| 29 | +See the [building and distribution](building_and_distribution.md#minimum-python-version-for-abi3) section for further detail. |
| 30 | + |
| 31 | +## Features for embedding Python in Rust |
| 32 | + |
| 33 | +### `auto-initalize` |
| 34 | + |
| 35 | +This feature changes [`Python::with_gil`](https://docs.rs/pyo3/latest/pyo3/struct.Python.html#method.with_gil) and [`Python::acquire_gil`](https://docs.rs/pyo3/latest/pyo3/struct.Python.html#method.acquire_gil) to automatically initialize a Python interpreter (by calling [`prepare_freethreaded_python`](https://docs.rs/pyo3/latest/pyo3/fn.prepare_freethreaded_python.html)) if needed. |
| 36 | + |
| 37 | +This feature is not needed for extension modules, but for compatibility it is enabled by default until at least the PyO3 0.14 release. |
| 38 | + |
| 39 | +> This feature is enabled by default. To disable it, set `default-features = false` for the `pyo3` entry in your Cargo.toml. |
| 40 | +
|
| 41 | +## Advanced Features |
| 42 | + |
| 43 | +### `macros` |
| 44 | + |
| 45 | +This feature enables a dependency on the `pyo3-macros` crate, which provides the procedural macros portion of PyO3's API: |
| 46 | + |
| 47 | +- `#[pymodule]` |
| 48 | +- `#[pyfunction]` |
| 49 | +- `#[pyclass]` |
| 50 | +- `#[pymethods]` |
| 51 | +- `#[pyproto]` |
| 52 | +- `#[derive(FromPyObject)]` |
| 53 | + |
| 54 | +It also provides the `py_run!` macro. |
| 55 | + |
| 56 | +These macros require a number of dependencies which may not be needed by users who just need PyO3 for Python FFI. Disabling this feature enables faster builds for those users, as these dependencies will not be built if this feature is disabled. |
| 57 | + |
| 58 | +> This feature is enabled by default. To disable it, set `default-features = false` for the `pyo3` entry in your Cargo.toml. |
| 59 | +
|
| 60 | +### `nightly` |
| 61 | + |
| 62 | +The `nightly` feature needs the nightly Rust compiler. This allows PyO3 to use Rust's unstable specialization feature to apply the following optimizations: |
| 63 | +- `FromPyObject` for `Vec` and `[T;N]` can perform a `memcpy` when the object supports the Python buffer protocol. |
| 64 | +- `ToBorrowedObject` can skip a reference count increase when the provided object is a Python native type. |
0 commit comments