maturin 1.4.0__py3-none-macosx_10_12_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of maturin might be problematic. Click here for more details.

maturin/__init__.py ADDED
@@ -0,0 +1,198 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ maturin's implementation of the PEP 517 interface. Calls maturin through subprocess
4
+
5
+ Currently, the "return value" of the rust implementation is the last line of stdout
6
+
7
+ On windows, apparently pip's subprocess handling sets stdout to some windows encoding (e.g. cp1252 on my machine),
8
+ even though the terminal supports utf8. Writing directly to the binary stdout buffer avoids encoding errors due to
9
+ maturin's emojis.
10
+ """
11
+ from __future__ import annotations
12
+
13
+ import os
14
+ import platform
15
+ import shlex
16
+ import shutil
17
+ import struct
18
+ import subprocess
19
+ import sys
20
+ from subprocess import SubprocessError
21
+ from typing import Any, Dict, Mapping, List, Optional
22
+
23
+ try:
24
+ import tomllib
25
+ except ModuleNotFoundError:
26
+ import tomli as tomllib # type: ignore
27
+
28
+
29
+ def get_config() -> Dict[str, str]:
30
+ with open("pyproject.toml", "rb") as fp:
31
+ pyproject_toml = tomllib.load(fp)
32
+ return pyproject_toml.get("tool", {}).get("maturin", {})
33
+
34
+
35
+ def get_maturin_pep517_args(config_settings: Optional[Mapping[str, Any]] = None) -> List[str]:
36
+ build_args = config_settings.get("build-args") if config_settings else None
37
+ if build_args is None:
38
+ env_args = os.getenv("MATURIN_PEP517_ARGS", "")
39
+ args = shlex.split(env_args)
40
+ elif isinstance(build_args, str):
41
+ args = shlex.split(build_args)
42
+ else:
43
+ args = build_args
44
+ return args
45
+
46
+
47
+ def _additional_pep517_args() -> List[str]:
48
+ # Support building for 32-bit Python on x64 Windows
49
+ if platform.system().lower() == "windows" and platform.machine().lower() == "amd64":
50
+ pointer_width = struct.calcsize("P") * 8
51
+ if pointer_width == 32:
52
+ return ["--target", "i686-pc-windows-msvc"]
53
+ return []
54
+
55
+
56
+ # noinspection PyUnusedLocal
57
+ def _build_wheel(
58
+ wheel_directory: str,
59
+ config_settings: Optional[Mapping[str, Any]] = None,
60
+ metadata_directory: Optional[str] = None,
61
+ editable: bool = False,
62
+ ) -> str:
63
+ # PEP 517 specifies that only `sys.executable` points to the correct
64
+ # python interpreter
65
+ command = [
66
+ "maturin",
67
+ "pep517",
68
+ "build-wheel",
69
+ "-i",
70
+ sys.executable,
71
+ "--compatibility",
72
+ "off",
73
+ ]
74
+ command.extend(_additional_pep517_args())
75
+ if editable:
76
+ command.append("--editable")
77
+
78
+ pep517_args = get_maturin_pep517_args(config_settings)
79
+ if pep517_args:
80
+ command.extend(pep517_args)
81
+
82
+ print("Running `{}`".format(" ".join(command)))
83
+ sys.stdout.flush()
84
+ result = subprocess.run(command, stdout=subprocess.PIPE)
85
+ sys.stdout.buffer.write(result.stdout)
86
+ sys.stdout.flush()
87
+ if result.returncode != 0:
88
+ sys.stderr.write(f"Error: command {command} returned non-zero exit status {result.returncode}\n")
89
+ sys.exit(1)
90
+ output = result.stdout.decode(errors="replace")
91
+ wheel_path = output.strip().splitlines()[-1]
92
+ filename = os.path.basename(wheel_path)
93
+ shutil.copy2(wheel_path, os.path.join(wheel_directory, filename))
94
+ return filename
95
+
96
+
97
+ # noinspection PyUnusedLocal
98
+ def build_wheel(
99
+ wheel_directory: str,
100
+ config_settings: Optional[Mapping[str, Any]] = None,
101
+ metadata_directory: Optional[str] = None,
102
+ ) -> str:
103
+ return _build_wheel(wheel_directory, config_settings, metadata_directory)
104
+
105
+
106
+ # noinspection PyUnusedLocal
107
+ def build_sdist(sdist_directory: str, config_settings: Optional[Mapping[str, Any]] = None) -> str:
108
+ command = ["maturin", "pep517", "write-sdist", "--sdist-directory", sdist_directory]
109
+
110
+ print("Running `{}`".format(" ".join(command)))
111
+ sys.stdout.flush()
112
+ result = subprocess.run(command, stdout=subprocess.PIPE)
113
+ sys.stdout.buffer.write(result.stdout)
114
+ sys.stdout.flush()
115
+ if result.returncode != 0:
116
+ sys.stderr.write(f"Error: command {command} returned non-zero exit status {result.returncode}\n")
117
+ sys.exit(1)
118
+ output = result.stdout.decode(errors="replace")
119
+ return output.strip().splitlines()[-1]
120
+
121
+
122
+ # noinspection PyUnusedLocal
123
+ def get_requires_for_build_wheel(config_settings: Optional[Mapping[str, Any]] = None) -> List[str]:
124
+ if get_config().get("bindings") == "cffi":
125
+ return ["cffi"]
126
+ else:
127
+ return []
128
+
129
+
130
+ # noinspection PyUnusedLocal
131
+ def build_editable(
132
+ wheel_directory: str,
133
+ config_settings: Optional[Mapping[str, Any]] = None,
134
+ metadata_directory: Optional[str] = None,
135
+ ) -> str:
136
+ return _build_wheel(wheel_directory, config_settings, metadata_directory, editable=True)
137
+
138
+
139
+ # Requirements to build an editable are the same as for a wheel
140
+ get_requires_for_build_editable = get_requires_for_build_wheel
141
+
142
+
143
+ # noinspection PyUnusedLocal
144
+ def get_requires_for_build_sdist(config_settings: Optional[Mapping[str, Any]] = None) -> List[str]:
145
+ return []
146
+
147
+
148
+ # noinspection PyUnusedLocal
149
+ def prepare_metadata_for_build_wheel(
150
+ metadata_directory: str, config_settings: Optional[Mapping[str, Any]] = None
151
+ ) -> str:
152
+ print("Checking for Rust toolchain....")
153
+ is_cargo_installed = False
154
+ try:
155
+ output = subprocess.check_output(["cargo", "--version"]).decode("utf-8", "ignore")
156
+ if "cargo" in output:
157
+ is_cargo_installed = True
158
+ except (FileNotFoundError, SubprocessError):
159
+ pass
160
+
161
+ if not is_cargo_installed:
162
+ sys.stderr.write(
163
+ "\nCargo, the Rust package manager, is not installed or is not on PATH.\n"
164
+ "This package requires Rust and Cargo to compile extensions. Install it through\n"
165
+ "the system's package manager or via https://rustup.rs/\n\n"
166
+ )
167
+ sys.exit(1)
168
+
169
+ command = [
170
+ "maturin",
171
+ "pep517",
172
+ "write-dist-info",
173
+ "--metadata-directory",
174
+ metadata_directory,
175
+ # PEP 517 specifies that only `sys.executable` points to the correct
176
+ # python interpreter
177
+ "--interpreter",
178
+ sys.executable,
179
+ ]
180
+ command.extend(_additional_pep517_args())
181
+ pep517_args = get_maturin_pep517_args(config_settings)
182
+ if pep517_args:
183
+ command.extend(pep517_args)
184
+
185
+ print("Running `{}`".format(" ".join(command)))
186
+ try:
187
+ _output = subprocess.check_output(command)
188
+ except subprocess.CalledProcessError as e:
189
+ sys.stderr.write(f"Error running maturin: {e}\n")
190
+ sys.exit(1)
191
+ sys.stdout.buffer.write(_output)
192
+ sys.stdout.flush()
193
+ output = _output.decode(errors="replace")
194
+ return output.strip().splitlines()[-1]
195
+
196
+
197
+ # Metadata for editable are the same as for a wheel
198
+ prepare_metadata_for_build_editable = prepare_metadata_for_build_wheel
maturin/__main__.py ADDED
@@ -0,0 +1,44 @@
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import sys
5
+ from pathlib import Path
6
+ import sysconfig
7
+ from typing import Optional
8
+
9
+
10
+ def get_maturin_path() -> Optional[Path]:
11
+ SCRIPT_NAME = "maturin"
12
+
13
+ def script_dir(scheme: str) -> str:
14
+ return sysconfig.get_path("scripts", scheme)
15
+
16
+ def script_exists(dir: str) -> bool:
17
+ for _, _, files in os.walk(dir):
18
+ for f in files:
19
+ name, *_ = os.path.splitext(f)
20
+ if name == SCRIPT_NAME:
21
+ return True
22
+
23
+ return False
24
+
25
+ paths = list(
26
+ filter(
27
+ script_exists,
28
+ filter(os.path.exists, map(script_dir, sysconfig.get_scheme_names())),
29
+ )
30
+ )
31
+
32
+ if paths:
33
+ return Path(paths[0]) / SCRIPT_NAME
34
+
35
+ return None
36
+
37
+
38
+ if __name__ == "__main__":
39
+ maturin = get_maturin_path()
40
+ if maturin is None:
41
+ print("Unable to find `maturin` script")
42
+ exit(1)
43
+
44
+ os.execv(maturin, [str(maturin)] + sys.argv[1:])
maturin/import_hook.py ADDED
@@ -0,0 +1,169 @@
1
+ from __future__ import annotations
2
+
3
+ import contextlib
4
+ import importlib
5
+ import importlib.util
6
+ import os
7
+ import pathlib
8
+ import shutil
9
+ import subprocess
10
+ import sys
11
+ from contextvars import ContextVar
12
+ from importlib import abc
13
+ from importlib.machinery import ModuleSpec
14
+ from types import ModuleType
15
+ from typing import Sequence
16
+
17
+ try:
18
+ import tomllib
19
+ except ModuleNotFoundError:
20
+ import tomli as tomllib # type: ignore
21
+
22
+
23
+ # Track if we have already built the package, so we can avoid infinite
24
+ # recursion.
25
+ _ALREADY_BUILT = ContextVar("_ALREADY_BUILT", default=False)
26
+
27
+
28
+ class Importer(abc.MetaPathFinder):
29
+ """A meta-path importer for the maturin based packages"""
30
+
31
+ def __init__(self, bindings: str | None = None, release: bool = False):
32
+ self.bindings = bindings
33
+ self.release = release
34
+
35
+ def find_spec(
36
+ self,
37
+ fullname: str,
38
+ path: Sequence[str | bytes] | None = None,
39
+ target: ModuleType | None = None,
40
+ ) -> ModuleSpec | None:
41
+ if fullname in sys.modules:
42
+ return None
43
+ if _ALREADY_BUILT.get():
44
+ # At this point we'll just import normally.
45
+ return None
46
+
47
+ mod_parts = fullname.split(".")
48
+ module_name = mod_parts[-1]
49
+
50
+ cwd = pathlib.Path(os.getcwd())
51
+ # Full Cargo project in cwd
52
+ cargo_toml = cwd / "Cargo.toml"
53
+ if _is_cargo_project(cargo_toml, module_name):
54
+ return self._build_and_load(fullname, cargo_toml)
55
+
56
+ # Full Cargo project in subdirectory of cwd
57
+ cargo_toml = cwd / module_name / "Cargo.toml"
58
+ if _is_cargo_project(cargo_toml, module_name):
59
+ return self._build_and_load(fullname, cargo_toml)
60
+ # module name with '-' instead of '_'
61
+ cargo_toml = cwd / module_name.replace("_", "-") / "Cargo.toml"
62
+ if _is_cargo_project(cargo_toml, module_name):
63
+ return self._build_and_load(fullname, cargo_toml)
64
+
65
+ # Single .rs file
66
+ rust_file = cwd / (module_name + ".rs")
67
+ if rust_file.exists():
68
+ project_dir = generate_project(rust_file, bindings=self.bindings or "pyo3")
69
+ cargo_toml = project_dir / "Cargo.toml"
70
+ return self._build_and_load(fullname, cargo_toml)
71
+
72
+ return None
73
+
74
+ def _build_and_load(self, fullname: str, cargo_toml: pathlib.Path) -> ModuleSpec | None:
75
+ build_module(cargo_toml, bindings=self.bindings)
76
+ loader = Loader(fullname)
77
+ return importlib.util.spec_from_loader(fullname, loader)
78
+
79
+
80
+ class Loader(abc.Loader):
81
+ def __init__(self, fullname: str):
82
+ self.fullname = fullname
83
+
84
+ def load_module(self, fullname: str) -> ModuleType:
85
+ # By the time we're loading, the package should've already been built
86
+ # by the previous step of finding the spec.
87
+ old_value = _ALREADY_BUILT.set(True)
88
+ try:
89
+ return importlib.import_module(self.fullname)
90
+ finally:
91
+ _ALREADY_BUILT.reset(old_value)
92
+
93
+
94
+ def _is_cargo_project(cargo_toml: pathlib.Path, module_name: str) -> bool:
95
+ with contextlib.suppress(FileNotFoundError):
96
+ with open(cargo_toml, "rb") as f:
97
+ cargo = tomllib.load(f)
98
+ package_name = cargo.get("package", {}).get("name")
99
+ if package_name == module_name or package_name.replace("-", "_") == module_name:
100
+ return True
101
+ return False
102
+
103
+
104
+ def generate_project(rust_file: pathlib.Path, bindings: str = "pyo3") -> pathlib.Path:
105
+ build_dir = pathlib.Path(os.getcwd()) / "build"
106
+ project_dir = build_dir / rust_file.stem
107
+ if project_dir.exists():
108
+ shutil.rmtree(project_dir)
109
+
110
+ command: list[str] = ["maturin", "new", "-b", bindings, str(project_dir)]
111
+ result = subprocess.run(command, stdout=subprocess.PIPE)
112
+ if result.returncode != 0:
113
+ sys.stderr.write(f"Error: command {command} returned non-zero exit status {result.returncode}\n")
114
+ raise ImportError("Failed to generate cargo project")
115
+
116
+ with open(rust_file) as f:
117
+ lib_rs_content = f.read()
118
+ lib_rs = project_dir / "src" / "lib.rs"
119
+ with open(lib_rs, "w") as f:
120
+ f.write(lib_rs_content)
121
+ return project_dir
122
+
123
+
124
+ def build_module(manifest_path: pathlib.Path, bindings: str | None = None, release: bool = False) -> None:
125
+ command = ["maturin", "develop", "-m", str(manifest_path)]
126
+ if bindings:
127
+ command.append("-b")
128
+ command.append(bindings)
129
+ if release:
130
+ command.append("--release")
131
+ result = subprocess.run(command, stdout=subprocess.PIPE)
132
+ sys.stdout.buffer.write(result.stdout)
133
+ sys.stdout.flush()
134
+ if result.returncode != 0:
135
+ sys.stderr.write(f"Error: command {command} returned non-zero exit status {result.returncode}\n")
136
+ raise ImportError("Failed to build module with maturin")
137
+
138
+
139
+ def _have_importer() -> bool:
140
+ for importer in sys.meta_path:
141
+ if isinstance(importer, Importer):
142
+ return True
143
+ return False
144
+
145
+
146
+ def install(bindings: str | None = None, release: bool = False) -> Importer | None:
147
+ """
148
+ Install the import hook.
149
+
150
+ :param bindings: Which kind of bindings to use.
151
+ Possible values are pyo3, rust-cpython and cffi
152
+
153
+ :param release: Build in release mode, otherwise debug mode by default
154
+ """
155
+ if _have_importer():
156
+ return None
157
+ importer = Importer(bindings=bindings, release=release)
158
+ sys.meta_path.insert(0, importer)
159
+ return importer
160
+
161
+
162
+ def uninstall(importer: Importer) -> None:
163
+ """
164
+ Uninstall the import hook.
165
+ """
166
+ try:
167
+ sys.meta_path.remove(importer)
168
+ except ValueError:
169
+ pass
Binary file
@@ -0,0 +1,343 @@
1
+ Metadata-Version: 2.1
2
+ Name: maturin
3
+ Version: 1.4.0
4
+ Classifier: Topic :: Software Development :: Build Tools
5
+ Classifier: Programming Language :: Rust
6
+ Classifier: Programming Language :: Python :: Implementation :: CPython
7
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
8
+ Requires-Dist: tomli >=1.1.0 ; python_version < '3.11'
9
+ Requires-Dist: ziglang ~=0.10.0 ; extra == 'zig'
10
+ Requires-Dist: patchelf ; extra == 'patchelf'
11
+ Provides-Extra: zig
12
+ Provides-Extra: patchelf
13
+ Summary: Build and publish crates with pyo3, rust-cpython and cffi bindings as well as rust binaries as python packages
14
+ Keywords: python,cffi,packaging,pypi,pyo3
15
+ Home-Page: https://github.com/pyo3/maturin
16
+ Author: konstin <konstin@mailbox.org>, messense <messense@icloud.com>
17
+ Author-email: konstin <konstin@mailbox.org>, messense <messense@icloud.com>
18
+ License: MIT OR Apache-2.0
19
+ Requires-Python: >=3.7
20
+ Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
21
+ Project-URL: Source Code, https://github.com/PyO3/maturin
22
+ Project-URL: Issues, https://github.com/PyO3/maturin/issues
23
+ Project-URL: Documentation, https://maturin.rs
24
+ Project-URL: Changelog, https://maturin.rs/changelog.html
25
+
26
+ # Maturin
27
+
28
+ _formerly pyo3-pack_
29
+
30
+ [![Maturin User Guide](https://img.shields.io/badge/user-guide-brightgreen?logo=readthedocs&style=flat-square)](https://maturin.rs)
31
+ [![Crates.io](https://img.shields.io/crates/v/maturin.svg?logo=rust&style=flat-square)](https://crates.io/crates/maturin)
32
+ [![PyPI](https://img.shields.io/pypi/v/maturin.svg?logo=python&style=flat-square)](https://pypi.org/project/maturin)
33
+ [![Actions Status](https://img.shields.io/github/actions/workflow/status/PyO3/maturin/test.yml?branch=main&logo=github&style=flat-square)](https://github.com/PyO3/maturin/actions)
34
+ [![FreeBSD](https://img.shields.io/cirrus/github/PyO3/maturin/main?logo=CircleCI&style=flat-square)](https://cirrus-ci.com/github/PyO3/maturin)
35
+ [![Chat on Gitter](https://img.shields.io/gitter/room/nwjs/nw.js.svg?logo=gitter&style=flat-square)](https://gitter.im/PyO3/Lobby)
36
+
37
+ Build and publish crates with pyo3, rust-cpython, cffi and uniffi bindings as well as rust binaries as python packages.
38
+
39
+ This project is meant as a zero configuration replacement for [setuptools-rust](https://github.com/PyO3/setuptools-rust) and [milksnake](https://github.com/getsentry/milksnake).
40
+ It supports building wheels for python 3.7+ on windows, linux, mac and freebsd, can upload them to [pypi](https://pypi.org/) and has basic pypy and graalpy support.
41
+
42
+ Check out the [User Guide](https://maturin.rs/)!
43
+
44
+ ## Usage
45
+
46
+ You can either download binaries from the [latest release](https://github.com/PyO3/maturin/releases/latest) or install it with [pipx](https://pypa.github.io/pipx/):
47
+
48
+ ```shell
49
+ pipx install maturin
50
+ ```
51
+
52
+ > **Note**
53
+ >
54
+ > `pip install maturin` should also work if you don't want to use pipx.
55
+
56
+ There are four main commands:
57
+
58
+ * `maturin new` creates a new cargo project with maturin configured.
59
+ * `maturin publish` builds the crate into python packages and publishes them to pypi.
60
+ * `maturin build` builds the wheels and stores them in a folder (`target/wheels` by default), but doesn't upload them. It's possible to upload those with [twine](https://github.com/pypa/twine) or `maturin upload`.
61
+ * `maturin develop` builds the crate and installs it as a python module directly in the current virtualenv. Note that while `maturin develop` is faster, it doesn't support all the feature that running `pip install` after `maturin build` supports.
62
+
63
+ `pyo3` and `rust-cpython` bindings are automatically detected. For cffi or binaries, you need to pass `-b cffi` or `-b bin`.
64
+ maturin doesn't need extra configuration files and doesn't clash with an existing setuptools-rust or milksnake configuration.
65
+ You can even integrate it with testing tools such as [tox](https://tox.readthedocs.io/en/latest/).
66
+ There are examples for the different bindings in the `test-crates` folder.
67
+
68
+ The name of the package will be the name of the cargo project, i.e. the name field in the `[package]` section of `Cargo.toml`.
69
+ The name of the module, which you are using when importing, will be the `name` value in the `[lib]` section (which defaults to the name of the package). For binaries, it's simply the name of the binary generated by cargo.
70
+
71
+ ## Python packaging basics
72
+
73
+ Python packages come in two formats:
74
+ A built form called wheel and source distributions (sdist), both of which are archives.
75
+ A wheel can be compatible with any python version, interpreter (cpython and pypy, mainly), operating system and hardware architecture (for pure python wheels),
76
+ can be limited to a specific platform and architecture (e.g. when using ctypes or cffi) or to a specific python interpreter and version on a specific architecture and operating system (e.g. with pyo3 and rust-cpython).
77
+
78
+ When using `pip install` on a package, pip tries to find a matching wheel and install that. If it doesn't find one, it downloads the source distribution and builds a wheel for the current platform,
79
+ which requires the right compilers to be installed. Installing a wheel is much faster than installing a source distribution as building wheels is generally slow.
80
+
81
+ When you publish a package to be installable with `pip install`, you upload it to [pypi](https://pypi.org/), the official package repository.
82
+ For testing, you can use [test pypi](https://test.pypi.org/) instead, which you can use with `pip install --index-url https://test.pypi.org/simple/`.
83
+ Note that for publishing for linux, [you need to use the manylinux docker container](#manylinux-and-auditwheel), while for publishing from your repository you can use the [PyO3/maturin-action github action](https://github.com/PyO3/maturin-action).
84
+
85
+ ## pyo3 and rust-cpython
86
+
87
+ For pyo3 and rust-cpython, maturin can only build packages for installed python versions. On linux and mac, all python versions in `PATH` are used.
88
+ If you don't set your own interpreters with `-i`, a heuristic is used to search for python installations.
89
+ On windows all versions from the python launcher (which is installed by default by the python.org installer) and all conda environments except base are used. You can check which versions are picked up with the `list-python` subcommand.
90
+
91
+ pyo3 will set the used python interpreter in the environment variable `PYTHON_SYS_EXECUTABLE`, which can be used from custom build scripts. Maturin can build and upload wheels for pypy with pyo3, even though only pypy3.7-7.3 on linux is tested.
92
+
93
+ ## Cffi
94
+
95
+ Cffi wheels are compatible with all python versions including pypy. If `cffi` isn't installed and python is running inside a virtualenv, maturin will install it, otherwise you have to install it yourself (`pip install cffi`).
96
+
97
+ maturin uses cbindgen to generate a header file, which can be customized by configuring cbindgen through a `cbindgen.toml` file inside your project root. Alternatively you can use a build script that writes a header file to `$PROJECT_ROOT/target/header.h`.
98
+
99
+ Based on the header file maturin generates a module which exports an `ffi` and a `lib` object.
100
+
101
+ <details>
102
+ <summary>Example of a custom build script</summary>
103
+
104
+ ```rust
105
+ use cbindgen;
106
+ use std::env;
107
+ use std::path::Path;
108
+
109
+ fn main() {
110
+ let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
111
+
112
+ let bindings = cbindgen::Builder::new()
113
+ .with_no_includes()
114
+ .with_language(cbindgen::Language::C)
115
+ .with_crate(crate_dir)
116
+ .generate()
117
+ .unwrap();
118
+ bindings.write_to_file(Path::new("target").join("header.h"));
119
+ }
120
+ ```
121
+
122
+ </details>
123
+
124
+ ## uniffi
125
+
126
+ uniffi bindings use [uniffi-rs](https://mozilla.github.io/uniffi-rs/) to generate Python `ctypes` bindings
127
+ from an interface definition file. uniffi wheels are compatible with all python versions including pypy.
128
+
129
+ ## Mixed rust/python projects
130
+
131
+ To create a mixed rust/python project, create a folder with your module name (i.e. `lib.name` in Cargo.toml) next to your Cargo.toml and add your python sources there:
132
+
133
+ ```
134
+ my-project
135
+ ├── Cargo.toml
136
+ ├── my_project
137
+ │   ├── __init__.py
138
+ │   └── bar.py
139
+ ├── pyproject.toml
140
+ ├── README.md
141
+ └── src
142
+    └── lib.rs
143
+ ```
144
+
145
+ You can specify a different python source directory in `pyproject.toml` by setting `tool.maturin.python-source`, for example
146
+
147
+ **pyproject.toml**
148
+
149
+ ```toml
150
+ [tool.maturin]
151
+ python-source = "python"
152
+ module-name = "my_project._lib_name"
153
+ ```
154
+
155
+ then the project structure would look like this:
156
+
157
+ ```
158
+ my-project
159
+ ├── Cargo.toml
160
+ ├── python
161
+ │ └── my_project
162
+ │ ├── __init__.py
163
+ │ └── bar.py
164
+ ├── pyproject.toml
165
+ ├── README.md
166
+ └── src
167
+    └── lib.rs
168
+ ```
169
+
170
+ > **Note**
171
+ >
172
+ > This structure is recommended to avoid [a common `ImportError` pitfall](https://github.com/PyO3/maturin/issues/490)
173
+
174
+ maturin will add the native extension as a module in your python folder. When using develop, maturin will copy the native library and for cffi also the glue code to your python folder. You should add those files to your gitignore.
175
+
176
+ With cffi you can do `from .my_project import lib` and then use `lib.my_native_function`, with pyo3/rust-cpython you can directly `from .my_project import my_native_function`.
177
+
178
+ Example layout with pyo3 after `maturin develop`:
179
+
180
+ ```
181
+ my-project
182
+ ├── Cargo.toml
183
+ ├── my_project
184
+ │   ├── __init__.py
185
+ │   ├── bar.py
186
+ │   └── _lib_name.cpython-36m-x86_64-linux-gnu.so
187
+ ├── README.md
188
+ └── src
189
+    └── lib.rs
190
+ ```
191
+
192
+ When doing this also be sure to set the module name in your code to match the last part of `module-name` (don't include the package path):
193
+
194
+ ```rust
195
+ #[pymodule]
196
+ #[pyo3(name="_lib_name")]
197
+ fn my_lib_name(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
198
+ m.add_class::<MyPythonRustClass>()?;
199
+ Ok(())
200
+ }
201
+ ```
202
+
203
+
204
+ ## Python metadata
205
+
206
+ maturin supports [PEP 621](https://www.python.org/dev/peps/pep-0621/), you can specify python package metadata in `pyproject.toml`.
207
+ maturin merges metadata from `Cargo.toml` and `pyproject.toml`, `pyproject.toml` takes precedence over `Cargo.toml`.
208
+
209
+ To specify python dependencies, add a list `dependencies` in a `[project]` section in the `pyproject.toml`. This list is equivalent to `install_requires` in setuptools:
210
+
211
+ ```toml
212
+ [project]
213
+ name = "my-project"
214
+ dependencies = ["flask~=1.1.0", "toml==0.10.0"]
215
+ ```
216
+
217
+ Pip allows adding so called console scripts, which are shell commands that execute some function in your program. You can add console scripts in a section `[project.scripts]`.
218
+ The keys are the script names while the values are the path to the function in the format `some.module.path:class.function`, where the `class` part is optional. The function is called with no arguments. Example:
219
+
220
+ ```toml
221
+ [project.scripts]
222
+ get_42 = "my_project:DummyClass.get_42"
223
+ ```
224
+
225
+ You can also specify [trove classifiers](https://pypi.org/classifiers/) in your `pyproject.toml` under `project.classifiers`:
226
+
227
+ ```toml
228
+ [project]
229
+ name = "my-project"
230
+ classifiers = ["Programming Language :: Python"]
231
+ ```
232
+
233
+ ## Source distribution
234
+
235
+ maturin supports building through `pyproject.toml`. To use it, create a `pyproject.toml` next to your `Cargo.toml` with the following content:
236
+
237
+ ```toml
238
+ [build-system]
239
+ requires = ["maturin>=1.0,<2.0"]
240
+ build-backend = "maturin"
241
+ ```
242
+
243
+ If a `pyproject.toml` with a `[build-system]` entry is present, maturin can build a source distribution of your package when `--sdist` is specified.
244
+ The source distribution will contain the same files as `cargo package`. To only build a source distribution, pass `--interpreter` without any values.
245
+
246
+ You can then e.g. install your package with `pip install .`. With `pip install . -v` you can see the output of cargo and maturin.
247
+
248
+ You can use the options `compatibility`, `skip-auditwheel`, `bindings`, `strip` and common Cargo build options such as `features` under `[tool.maturin]` the same way you would when running maturin directly.
249
+ The `bindings` key is required for cffi and bin projects as those can't be automatically detected. Currently, all builds are in release mode (see [this thread](https://discuss.python.org/t/pep-517-debug-vs-release-builds/1924) for details).
250
+
251
+ For a non-manylinux build with cffi bindings you could use the following:
252
+
253
+ ```toml
254
+ [build-system]
255
+ requires = ["maturin>=1.0,<2.0"]
256
+ build-backend = "maturin"
257
+
258
+ [tool.maturin]
259
+ bindings = "cffi"
260
+ compatibility = "linux"
261
+ ```
262
+
263
+ `manylinux` option is also accepted as an alias of `compatibility` for backward compatibility with old version of maturin.
264
+
265
+ To include arbitrary files in the sdist for use during compilation specify `include` as an array of `path` globs with `format` set to `sdist`:
266
+
267
+ ```toml
268
+ [tool.maturin]
269
+ include = [{ path = "path/**/*", format = "sdist" }]
270
+ ```
271
+
272
+ There's a `maturin sdist` command for only building a source distribution as workaround for [pypa/pip#6041](https://github.com/pypa/pip/issues/6041).
273
+
274
+ ## Manylinux and auditwheel
275
+
276
+ For portability reasons, native python modules on linux must only dynamically link a set of very few libraries which are installed basically everywhere, hence the name manylinux.
277
+ The pypa offers special docker images and a tool called [auditwheel](https://github.com/pypa/auditwheel/) to ensure compliance with the [manylinux rules](https://peps.python.org/pep-0599/#the-manylinux2014-policy).
278
+ If you want to publish widely usable wheels for linux pypi, **you need to use a manylinux docker image**.
279
+
280
+ The Rust compiler since version 1.64 [requires at least glibc 2.17](https://blog.rust-lang.org/2022/08/01/Increasing-glibc-kernel-requirements.html), so you need to use at least manylinux2014.
281
+ For publishing, we recommend enforcing the same manylinux version as the image with the manylinux flag, e.g. use `--manylinux 2014` if you are building in `quay.io/pypa/manylinux2014_x86_64`.
282
+ The [PyO3/maturin-action](https://github.com/PyO3/maturin-action) github action already takes care of this if you set e.g. `manylinux: 2014`.
283
+
284
+ maturin contains a reimplementation of auditwheel automatically checks the generated library and gives the wheel the proper.
285
+ If your system's glibc is too new or you link other shared libraries, it will assign the `linux` tag.
286
+ You can also manually disable those checks and directly use native linux target with `--manylinux off`.
287
+
288
+ For full manylinux compliance you need to compile in a CentOS docker container. The [pyo3/maturin](https://ghcr.io/pyo3/maturin) image is based on the manylinux2014 image,
289
+ and passes arguments to the `maturin` binary. You can use it like this:
290
+
291
+ ```
292
+ docker run --rm -v $(pwd):/io ghcr.io/pyo3/maturin build --release # or other maturin arguments
293
+ ```
294
+
295
+ Note that this image is very basic and only contains python, maturin and stable rust. If you need additional tools, you can run commands inside the manylinux container.
296
+ See [konstin/complex-manylinux-maturin-docker](https://github.com/konstin/complex-manylinux-maturin-docker) for a small educational example or [nanoporetech/fast-ctc-decode](https://github.com/nanoporetech/fast-ctc-decode/blob/b226ea0f2b2f4f474eff47349703d57d2ea4801b/.github/workflows/publish.yml) for a real world setup.
297
+
298
+ maturin itself is manylinux compliant when compiled for the musl target.
299
+
300
+ ## Examples
301
+
302
+ * [ballista-python](https://github.com/apache/arrow-ballista-python) - A Python library that binds to Apache Arrow distributed query engine Ballista
303
+ * [chardetng-py](https://github.com/john-parton/chardetng-py) - Python binding for the chardetng character encoding detector.
304
+ * [connector-x](https://github.com/sfu-db/connector-x/tree/main/connectorx-python) - ConnectorX enables you to load data from databases into Python in the fastest and most memory efficient way
305
+ * [datafusion-python](https://github.com/apache/arrow-datafusion-python) - a Python library that binds to Apache Arrow in-memory query engine DataFusion
306
+ * [deltalake-python](https://github.com/delta-io/delta-rs/tree/main/python) - Native Delta Lake Python binding based on delta-rs with Pandas integration
307
+ * [opendal](https://github.com/apache/incubator-opendal/tree/main/bindings/python) - OpenDAL Python Binding to access data freely
308
+ * [orjson](https://github.com/ijl/orjson) - A fast, correct JSON library for Python
309
+ * [polars](https://github.com/pola-rs/polars/tree/master/py-polars) - Fast multi-threaded DataFrame library in Rust | Python | Node.js
310
+ * [pydantic-core](https://github.com/pydantic/pydantic-core) - Core validation logic for pydantic written in Rust
311
+ * [pyrus-cramjam](https://github.com/milesgranger/pyrus-cramjam) - Thin Python wrapper to de/compression algorithms in Rust
312
+ * [pyxel](https://github.com/kitao/pyxel) - A retro game engine for Python
313
+ * [roapi](https://github.com/roapi/roapi) - ROAPI automatically spins up read-only APIs for static datasets without requiring you to write a single line of code
314
+ * [robyn](https://github.com/sansyrox/robyn) - A fast and extensible async python web server with a Rust runtime
315
+ * [ruff](https://github.com/charliermarsh/ruff) - An extremely fast Python linter, written in Rust
316
+ * [tantivy-py](https://github.com/quickwit-oss/tantivy-py) - Python bindings for Tantivy
317
+ * [watchfiles](https://github.com/samuelcolvin/watchfiles) - Simple, modern and high performance file watching and code reload in python
318
+ * [wonnx](https://github.com/webonnx/wonnx/tree/master/wonnx-py) - Wonnx is a GPU-accelerated ONNX inference run-time written 100% in Rust
319
+
320
+ ## Contributing
321
+
322
+ Everyone is welcomed to contribute to maturin! There are many ways to support the project, such as:
323
+
324
+ - help maturin users with issues on GitHub and Gitter
325
+ - improve documentation
326
+ - write features and bugfixes
327
+ - publish blogs and examples of how to use maturin
328
+
329
+ Our [contributing notes](https://github.com/PyO3/maturin/blob/main/guide/src/contributing.md) have more resources if you wish to volunteer time for maturin and are searching where to start.
330
+
331
+ If you don't have time to contribute yourself but still wish to support the project's future success, some of our maintainers have GitHub sponsorship pages:
332
+
333
+ - [messense](https://github.com/sponsors/messense)
334
+
335
+ ## License
336
+
337
+ Licensed under either of:
338
+
339
+ * Apache License, Version 2.0, ([LICENSE-APACHE](https://github.com/PyO3/maturin/blob/main/license-apache) or http://www.apache.org/licenses/LICENSE-2.0)
340
+ * MIT license ([LICENSE-MIT](https://github.com/PyO3/maturin/blob/main/license-mit) or http://opensource.org/licenses/MIT)
341
+
342
+ at your option.
343
+
@@ -0,0 +1,7 @@
1
+ maturin-1.4.0.dist-info/METADATA,sha256=HWd6-fAdyZTnZ1vSQVKvv6upCbhbvDrO_YkZLErcOr8,18382
2
+ maturin-1.4.0.dist-info/WHEEL,sha256=XZs7YqiZZBVwjUJFDH_4SF9SM8QS2rSqm6iPIRrU0Oc,103
3
+ maturin/__init__.py,sha256=aIJTh4e2q5Ixr3r7rZRKx6wz0iuPcGutvCqPaBCYVUA,6522
4
+ maturin/import_hook.py,sha256=4Uwi4mBhMh3OlrWYpz2bwRU1mBG7HP2YObpo3vPcNYI,5602
5
+ maturin/__main__.py,sha256=4CyZAGqzmFsCl0FMlxAcG4vHFepE2lRZiPu4MKXyY4Y,987
6
+ maturin-1.4.0.data/scripts/maturin,sha256=RbyHVpSWdD81hV0L3NXLSrsmmrQ-dvMve7Utsw0Y6rQ,21544480
7
+ maturin-1.4.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: maturin (1.4.0)
3
+ Root-Is-Purelib: false
4
+ Tag: py3-none-macosx_10_12_x86_64