You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+39-18Lines changed: 39 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -24,15 +24,34 @@ You can use PyO3 to write a native Python module in Rust, or to embed Python in
24
24
25
25
### Using Rust from Python
26
26
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.
28
28
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:
30
49
31
50
**`Cargo.toml`**
32
51
33
52
```toml
34
53
[package]
35
-
name = "string-sum"
54
+
name = "string_sum"
36
55
version = "0.1.0"
37
56
edition = "2018"
38
57
@@ -45,9 +64,8 @@ name = "string_sum"
45
64
# crate-type = ["cdylib", "rlib"]
46
65
crate-type = ["cdylib"]
47
66
48
-
[dependencies.pyo3]
49
-
version = "0.15.1"
50
-
features = ["extension-module"]
67
+
[dependencies]
68
+
pyo3 = { version = "0.15.1", features = ["extension-module"] }
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`:
85
93
86
94
```bash
87
95
$ maturin develop
@@ -92,7 +100,20 @@ $ python
92
100
'25'
93
101
```
94
102
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.
0 commit comments