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-16Lines changed: 39 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -20,17 +20,13 @@ PyO3 supports Python 3.6 and up. The minimum required Rust version is 1.41.
20
20
21
21
PyPy is also supported. Some minor features are unavailable on PyPy - please refer to the [pypy section in the guide](https://pyo3.rs/latest/building_and_distribution/pypy.html) for more information.
22
22
23
-
You can either write a native Python module in Rust, or use Python from a Rust binary.
23
+
You can use PyO3 to write a native Python module in Rust, or to embed Python in a Rust binary. The following sections explain each of these in turn.
24
24
25
-
However, on some OSs, you need some additional packages. E.g. if you are on _Ubuntu 18.04_, please run
25
+
### Using Rust from Python
26
26
27
-
```bash
28
-
sudo apt install python3-dev python-dev
29
-
```
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.
30
28
31
-
## Using Rust from Python
32
-
33
-
PyO3 can be used to generate a native Python module.
29
+
First, create a new folder (let's call it `string_sum`) containing the following two files:
While developing, you can symlink (or copy) and rename the shared library from the target folder: On MacOS, rename `libstring_sum.dylib` to `string_sum.so`, on Windows `libstring_sum.dll` to `string_sum.pyd`, and on Linux `libstring_sum.so` to `string_sum.so`. Then open a Python shell in the same folder and you'll be able to `import string_sum`.
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
+
```
78
83
79
-
To build, test and publish your crate as a Python module, you can use [maturin](https://github.com/PyO3/maturin) or [setuptools-rust](https://github.com/PyO3/setuptools-rust). You can find an example for setuptools-rust in [examples/word-count](https://github.com/PyO3/pyo3/tree/main/examples/word-count), while maturin should work on your crate without any configuration.
84
+
Now buildand execute the module:
80
85
81
-
## Using Python from Rust
86
+
```bash
87
+
$ maturin develop
88
+
# lots of progress output as maturin runs the compilation...
89
+
$ python
90
+
>>> import string_sum
91
+
>>> string_sum.sum_as_string(5, 20)
92
+
'25'
93
+
```
94
+
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.
96
+
97
+
### Using Python from Rust
98
+
99
+
To embed Python into a Rust binary, you need to ensure that your Python installation contains a shared library. The following steps demonstrate how to ensure this (for Ubuntu), and then give some example code which runs an embedded Python interpreter.
100
+
101
+
To install the Python shared library, if you are on Ubuntu, you can run:
102
+
103
+
```bash
104
+
sudo apt install python3-dev
105
+
```
82
106
83
-
If you want your Rust application to create a Python interpreter internally and
84
-
use it to run Python code, add `pyo3` to your `Cargo.toml` like this:
107
+
Start a new project with `cargo new`. Next, add `pyo3` to your `Cargo.toml` like this:
0 commit comments