{"id":13477140,"url":"https://github.com/PyO3/setuptools-rust","last_synced_at":"2025-03-27T04:32:31.034Z","repository":{"id":37686514,"uuid":"84393574","full_name":"PyO3/setuptools-rust","owner":"PyO3","description":"Setuptools plugin for Rust support","archived":false,"fork":false,"pushed_at":"2025-03-14T20:36:24.000Z","size":1262,"stargazers_count":620,"open_issues_count":7,"forks_count":103,"subscribers_count":15,"default_branch":"main","last_synced_at":"2025-03-25T22:15:16.420Z","etag":null,"topics":["python","rust","setuptools"],"latest_commit_sha":null,"homepage":"https://setuptools-rust.readthedocs.io","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PyO3.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-09T03:24:55.000Z","updated_at":"2025-03-22T14:30:53.000Z","dependencies_parsed_at":"2024-02-01T22:45:43.416Z","dependency_job_id":"d7051eaf-ef97-46ca-9d51-348673630215","html_url":"https://github.com/PyO3/setuptools-rust","commit_stats":{"total_commits":387,"total_committers":45,"mean_commits":8.6,"dds":0.7545219638242894,"last_synced_commit":"684bf63aed2a041a522517e0f0ed0f7450ac979a"},"previous_names":[],"tags_count":51,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PyO3%2Fsetuptools-rust","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PyO3%2Fsetuptools-rust/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PyO3%2Fsetuptools-rust/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PyO3%2Fsetuptools-rust/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PyO3","download_url":"https://codeload.github.com/PyO3/setuptools-rust/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245550679,"owners_count":20633883,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["python","rust","setuptools"],"created_at":"2024-07-31T16:01:38.708Z","updated_at":"2025-03-27T04:32:31.019Z","avatar_url":"https://github.com/PyO3.png","language":"Python","readme":"# Setuptools plugin for Rust extensions\n\n[![github actions](https://github.com/PyO3/setuptools-rust/actions/workflows/ci.yml/badge.svg)](https://github.com/PyO3/setuptools-rust/actions/workflows/ci.yml)\n[![pypi package](https://badge.fury.io/py/setuptools-rust.svg)](https://pypi.org/project/setuptools-rust/)\n[![readthedocs](https://readthedocs.org/projects/pip/badge/)](https://setuptools-rust.readthedocs.io/en/latest/)\n[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n\n`setuptools-rust` is a plugin for `setuptools` to build Rust Python extensions implemented with [PyO3](https://github.com/PyO3/pyo3) or [rust-cpython](https://github.com/dgrunwald/rust-cpython).\n\nCompile and distribute Python extensions written in Rust as easily as if\nthey were written in C.\n\n## Quickstart\n\nThe following is a very basic tutorial that shows how to use `setuptools-rust` in `pyproject.toml`.\nIt assumes that you already have a bunch of Python and Rust files that you want\nto distribute. You can see examples for these files in the\n[`examples/hello-world`](https://github.com/PyO3/setuptools-rust/tree/main/examples/hello-world)\ndirectory in the [github repository](https://github.com/PyO3/setuptools-rust).\nThe [PyO3 docs](https://pyo3.rs) have detailed information on how to write Python\nmodules in Rust.\n\n```\nhello-world\n├── python\n│   └── hello_world\n│       └── __init__.py\n└── rust\n    └── lib.rs\n```\n\nOnce the implementation files are in place, we need to add a `pyproject.toml`\nfile that tells anyone that wants to use your project how to build it.\nIn this file, we use an [array of tables](https://toml.io/en/v1.0.0#array-of-tables)\n(TOML jargon equivalent to Python's list of dicts) for ``[[tool.setuptools-rust.ext-modules]]``,\nto specify different extension modules written in Rust:\n\n\n```toml\n# pyproject.toml\n[build-system]\nrequires = [\"setuptools\", \"setuptools-rust\"]\nbuild-backend = \"setuptools.build_meta\"\n\n[project]\nname = \"hello-world\"\nversion = \"1.0\"\n\n[tool.setuptools.packages]\n# Pure Python packages/modules\nfind = { where = [\"python\"] }\n\n[[tool.setuptools-rust.ext-modules]]\n# Private Rust extension module to be nested into the Python package\ntarget = \"hello_world._lib\"  # The last part of the name (e.g. \"_lib\") has to match lib.name in Cargo.toml,\n                             # but you can add a prefix to nest it inside of a Python package.\npath = \"Cargo.toml\"      # Default value, can be omitted\nbinding = \"PyO3\"         # Default value, can be omitted\n```\n\nEach extension module should map directly into the corresponding `[lib]` table on the\n[Cargo manifest file](https://doc.rust-lang.org/cargo/reference/manifest.html):\n\n```toml\n# Cargo.toml\n[package]\nname = \"hello-world\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[dependencies]\npyo3 = \"0.24\"\n\n[lib]\nname = \"_lib\"  # private module to be nested into Python package,\n               # needs to match the name of the function with the `[#pymodule]` attribute\npath = \"rust/lib.rs\"\ncrate-type = [\"cdylib\"]  # required for shared library for Python to import from.\n\n# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html\n# See also PyO3 docs on writing Cargo.toml files at https://pyo3.rs\n```\n\nYou will also need to tell Setuptools that the Rust files are required to build your\nproject from the [source distribution](https://setuptools.pypa.io/en/latest/userguide/miscellaneous.html).\nThat can be done either via `MANIFEST.in` (see example below) or via a plugin like\n[`setuptools-scm`](https://pypi.org/project/setuptools-scm/).\n\n```\n# MANIFEST.in\ninclude Cargo.toml\nrecursive-include rust *.rs\n```\n\nWith these files in place, you can install the project in a virtual environment\nfor testing and making sure everything is working correctly:\n\n```powershell\n# cd hello-world\npython3 -m venv .venv\nsource .venv/bin/activate  # on Linux or macOS\n.venv\\Scripts\\activate     # on Windows\npython -m pip install -e .\npython\n\u003e\u003e\u003e import hello_world\n# ... try running something from your new extension module ...\n# ... better write some tests with pytest ...\n```\n\n## Next steps and final remarks\n\n- When you are ready to distribute your project, have a look on\n  [the notes in the documentation about building wheels](https://setuptools-rust.readthedocs.io/en/latest/building_wheels.html).\n\n- Cross-compiling is also supported, using one of\n  [`crossenv`](https://github.com/benfogle/crossenv),\n  [`cross`](https://github.com/rust-embedded/cross) or\n  [`cargo-zigbuild`](https://github.com/messense/cargo-zigbuild).\n  For examples see the `test-crossenv` and `test-cross` and `test-zigbuild` Github actions jobs in\n  [`ci.yml`](https://github.com/PyO3/setuptools-rust/blob/main/.github/workflows/ci.yml).\n\n- You can also use `[[tool.setuptools-rust.bins]]` (instead of `[[tool.setuptools-rust.ext-modules]]`),\n  if you want to distribute a binary executable written in Rust (instead of a library that can be imported by the Python runtime).\n  Note however that distributing both library and executable (or multiple executables),\n  may significantly increase the size of the\n  [wheel](https://packaging.python.org/en/latest/glossary/#term-Wheel)\n  file distributed by the\n  [package index](https://packaging.python.org/en/latest/glossary/#term-Package-Index)\n  and therefore increase build, download and installation times.\n  Another approach is to use a Python entry-point that calls the Rust\n  implementation (exposed via PyO3 bindings).\n  See the [hello-world](https://github.com/PyO3/setuptools-rust/tree/main/examples/hello-world)\n  example for more insights.\n\n- For a complete reference of the configuration options, see the\n  [API reference](https://setuptools-rust.readthedocs.io/en/latest/reference.html).\n  You can use any parameter defined by the `RustExtension` class with\n  `[[tool.setuptools-rust.ext-modules]]` and any parameter defined by the\n  `RustBin` class with `[[tool.setuptools-rust.bins]]`; just remember to replace\n  underscore characters `_` with dashes `-` in your `pyproject.toml` file.\n\n- `Cargo.toml` allow only one `[lib]` table per file.\n  If you require multiple extension modules you will need to write multiple `Cargo.toml` files.\n  Alternatively you can create a single private Rust top-level module that exposes\n  multiple submodules (using [PyO3's submodules](https://pyo3.rs/v0.20.0/module#python-submodules)),\n  which may also reduce the size of the build artifacts.\n  You can always keep your extension modules private and wrap them in pure Python\n  to have fine control over the public API.\n\n- If want to include both `[[tool.setuptools-rust.bins]]` and `[[tool.setuptools-rust.ext-modules]]`\n  in the same macOS wheel, you might have to manually add an extra `build.rs` file,\n  see [PyO3/setuptools-rust#351](https://github.com/PyO3/setuptools-rust/pull/351)\n  for more information about the workaround.\n\n- For more examples, see:\n  - [`hello-world`](https://github.com/PyO3/setuptools-rust/tree/main/examples/hello-world):\n    a more complete version of the code used in this tutorial that mixes both\n    `[[tool.setuptools-rust.ext-modules]]` and `[[tool.setuptools-rust.bins]]`\n    in a single distribution.\n  - [`html-py-ever`](https://github.com/PyO3/setuptools-rust/tree/main/examples/html-py-ever):\n    a more advanced example that uses Rust crates as dependencies.\n  - [`rust_with_cffi`](https://github.com/PyO3/setuptools-rust/tree/main/examples/rust_with_cffi):\n    uses both Rust and [CFFI](https://cffi.readthedocs.io/en/latest/).\n  - [`namespace_package`](https://github.com/PyO3/setuptools-rust/tree/main/examples/namespace_package):\n    integrates Rust-written modules into PEP 420 namespace packages.\n  - [`hello-world-script`](https://github.com/PyO3/setuptools-rust/tree/main/examples/hello-world-script):\n    uses Rust only for creating binary executables, not library modules.\n","funding_links":[],"categories":["Python","Build Tools \u0026 Frameworks"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPyO3%2Fsetuptools-rust","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FPyO3%2Fsetuptools-rust","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPyO3%2Fsetuptools-rust/lists"}