Skip to content

Commit 6ffa508

Browse files
committed
readme: recommend maturin init
1 parent 770c02e commit 6ffa508

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed

README.md

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,34 @@ You can use PyO3 to write a native Python module in Rust, or to embed Python in
2424

2525
### Using Rust from Python
2626

27-
PyO3 can be used to generate a native Python module. The easiest way to try this out for the first time is to use [`maturin`](https://github.com/PyO3/maturin). `maturin` is a tool for building and publishing Rust-based Python packages with minimal configuration. The following steps set up some files for an example Python module, install `maturin`, and then show how build and import the Python module.
27+
PyO3 can be used to generate a native Python module. The easiest way to try this out for the first time is to use [`maturin`](https://github.com/PyO3/maturin). `maturin` is a tool for building and publishing Rust-based Python packages with minimal configuration. The following steps install `maturin`, use it to generate and build a new Python package, and then launch Python to import and execute a function from the package.
2828

29-
First, create a new folder (let's call it `string_sum`) containing the following two files:
29+
First, follow the commands below to create a new directory containing a new Python `virtualenv`, and install `maturin` into the virtualenv using Python's package manager, `pip`:
30+
31+
```bash
32+
# (replace string_sum with the desired package name)
33+
$ mkdir string_sum
34+
$ cd string_sum
35+
$ python -m venv .env
36+
$ source .env/bin/activate
37+
$ pip install maturin
38+
```
39+
40+
Still inside this `string_sum` directory, now run `maturin init`. This will generate the new package source. When given the choice of bindings to use, select pyo3 bindings:
41+
42+
```bash
43+
$ maturin init
44+
✔ 🤷 What kind of bindings to use? · pyo3
45+
✨ Done! New project created string_sum
46+
```
47+
48+
The most important files generated by this command are `Cargo.toml` and `lib.rs`, which will look roughly like the following:
3049

3150
**`Cargo.toml`**
3251

3352
```toml
3453
[package]
35-
name = "string-sum"
54+
name = "string_sum"
3655
version = "0.1.0"
3756
edition = "2018"
3857

@@ -45,9 +64,8 @@ name = "string_sum"
4564
# crate-type = ["cdylib", "rlib"]
4665
crate-type = ["cdylib"]
4766

48-
[dependencies.pyo3]
49-
version = "0.15.1"
50-
features = ["extension-module"]
67+
[dependencies]
68+
pyo3 = { version = "0.15.1", features = ["extension-module"] }
5169
```
5270

5371
**`src/lib.rs`**
@@ -67,21 +85,11 @@ fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
6785
#[pymodule]
6886
fn string_sum(_py: Python, m: &PyModule) -> PyResult<()> {
6987
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
70-
7188
Ok(())
7289
}
7390
```
7491

75-
With those two files in place, now `maturin` needs to be installed. This can be done using Python's package manager `pip`. First, load up a new Python `virtualenv`, and install `maturin` into it:
76-
77-
```bash
78-
$ cd string_sum
79-
$ python -m venv .env
80-
$ source .env/bin/activate
81-
$ pip install maturin
82-
```
83-
84-
Now build and execute the module:
92+
Finally, run `maturin develop`. This will build the package and install it into the Python virtualenv previously created and activated. The package is then ready to be used from `python`:
8593

8694
```bash
8795
$ maturin develop
@@ -92,7 +100,20 @@ $ python
92100
'25'
93101
```
94102

95-
As well as with `maturin`, it is possible to build using [`setuptools-rust`](https://github.com/PyO3/setuptools-rust) or [manually](https://pyo3.rs/latest/building_and_distribution.html#manual-builds). Both offer more flexibility than `maturin` but require further configuration.
103+
To make changes to the package, just edit the Rust source code and then re-run `maturin develop` to recompile.
104+
105+
To run this all as a single copy-and-paste, use the bash script below (replace `string_sum` in the first command with the desired package name):
106+
107+
```bash
108+
mkdir string_sum && cd "$_"
109+
python -m venv .env
110+
source .env/bin/activate
111+
pip install maturin
112+
maturin init --bindings pyo3
113+
maturin develop
114+
```
115+
116+
As well as with `maturin`, it is possible to build using [`setuptools-rust`](https://github.com/PyO3/setuptools-rust) or [manually](https://pyo3.rs/latest/building_and_distribution.html#manual-builds). Both offer more flexibility than `maturin` but require more configuration to get started.
96117

97118
### Using Python from Rust
98119

0 commit comments

Comments
 (0)