Table of contents

Packaging Compiled Projects

There are a variety of ways to package compiled projects. In the past, the only way to do it was to use setuptools/distutils, which required using lots of fragile internals - distutils was intended primarily to compile CPython, and setuptools tried to stay away from changing the compile portions except as required. Now, however, we have several very nice options for compiled packages!

The most exciting developments have been new native build backends:

  • scikit-build-core: Builds C/C++/Fortran using CMake.
  • meson-python: Builds C/C++/Fortran using Meson.
  • maturin: Builds Rust using Cargo. Written entirely in Rust!
  • enscons: Builds C/C++ using SCONs. (Aging now, but this was the first native backend!)

You should be familiar with packing a pure Python project - the metadata configuration is the same.

There are also classic setuptools plugins:

If you have a really complex build, the newer native build backends might not support your use case yet, but if that’s the case, ask - development is driven by community needs. The older, more fragile setuptools based plugins are still a bit more flexible if you really need that flexibility for a feature not yet implemented in the native backends.

pyproject.toml: build-system

PY001 Packages must have a pyproject.toml file PP001 that selects the backend:

requires = ["scikit-build-core"]
build-backend = ""

pyproject.toml: project table

The metadata is specified in a standards-based format:

name = "package"
description = "A great package."
readme = ""
license.file = "LICENSE"
authors = [
  { name = "My Name", email = "" },
maintainers = [
  { name = "My Organization", email = "" },
requires-python = ">=3.8"

dependencies = [

classifiers = [
  "Development Status :: 4 - Beta",
  "License :: OSI Approved :: BSD License",
  "Programming Language :: Python :: 3 :: Only",
  "Programming Language :: Python :: 3.8",
  "Programming Language :: Python :: 3.9",
  "Programming Language :: Python :: 3.10",
  "Programming Language :: Python :: 3.11",
  "Programming Language :: Python :: 3.12",
  "Topic :: Scientific/Engineering :: Physics",

Homepage = ""
Documentation = ""
"Bug Tracker" = ""
Discussions = ""
Changelog = ""

You can read more about each field, and all allowed fields, in, Flit or Whey. Note that “Homepage” is special, and replaces the old url setting.


It is recommended to use extras instead of or in addition to making requirement files. These extras a) correctly interact with install requires and other built-in tools, b) are available directly when installing via PyPI, and c) are allowed in requirements.txt, install_requires, pyproject.toml, and most other places requirements are passed.

Here is an example of a simple extras:

test = [
  "pytest >=6.0",
mpl = [
  "matplotlib >=2.0",

Self dependencies can be used by using the name of the package, such as dev = ["package[test,examples]"], but this requires Pip 21.2 or newer. We recommend providing at least test and docs.

Command line

If you want to ship an “app” that a user can run from the command line, you need to add a script entry point. The form is:

cliapp = "package.__main__:main"

The format is command line app name as the key, and the value is the path to the function, followed by a colon, then the function to call. If you use as the file, then python -m followed by the module will also work to call the app (__name__ will be "__main__" in that case).

Tool section in pyproject.toml

These tools all read the project table. They also have extra configuration options in tool.* settings.

Backend specific files

Example CMakeLists.txt file (using pybind11, so include pybind11 in build-system.requires too):

cmake_minimum_required(VERSION 3.15...3.26)

find_package(pybind11 CONFIG REQUIRED)

pybind11_add_module(_core MODULE src/main.cpp)

Example compiled file

Example src/main.cpp file:

#include <pybind11/pybind11.h>

int add(int i, int j) { return i + j; }

namespace py = pybind11;

PYBIND11_MODULE(_core, m) {
  m.doc() = R"pbdoc(
      Pybind11 example plugin
      .. currentmodule:: python_example
      .. autosummary::
         :toctree: _generate

  m.def("add", &add, R"pbdoc(
      Add two numbers
      Some other explanation about the add function.

  m.def("subtract", [](int i, int j) { return i - j; }, R"pbdoc(
      Subtract two numbers
      Some other explanation about the subtract function.

Package structure

The recommendation (followed above) is to have source code in /src, and the Python package files in /src/<package>.


Check the documentation for the tools above to see what forms of dynamic versioning the tool supports.

Including/excluding files in the SDist

Each tool uses a different mechanism to include or remove files from the SDist, though the defaults are reasonable.


Unlike pure Python, you’ll need to build redistributable wheels for each platform and supported Python version if you want to avoid compilation on the user’s system. See the CI page on wheels for a suggested workflow.

Special considerations


Modern versions of NumPy (1.25+) allow you to target older versions when building, which is highly recommended, and this will become required in NumPy 2.0. Now you add:


(Where that number is whatever version you support as a minimum) then make sure you build with NumPy 1.25+ (or 2.0+ when it comes out). Before 1.25, it was necessary to actually pin the oldest NumPy you supported (the oldest-supported-numpy package is the easiest method). If you support Python < 3.9, you’ll have to use the old method for those versions.

If using pybind11, you don’t need NumPy at build-time in the first place.