Skip to content

Commit fb744e1

Browse files
authored
Merge pull request #1743 from davidhewitt/improve-readme
readme: slightly improve getting started notes
2 parents e6d750a + 13f8953 commit fb744e1

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

README.md

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,13 @@ PyO3 supports Python 3.6 and up. The minimum required Rust version is 1.41.
2020

2121
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.
2222

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.
2424

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
2626

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.
3028

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:
3430

3531
**`Cargo.toml`**
3632

@@ -65,23 +61,50 @@ fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
6561
Ok((a + b).to_string())
6662
}
6763

68-
/// A Python module implemented in Rust.
64+
/// A Python module implemented in Rust. The name of this function must match
65+
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
66+
/// import the module.
6967
#[pymodule]
70-
fn string_sum(py: Python, m: &PyModule) -> PyResult<()> {
68+
fn string_sum(_py: Python, m: &PyModule) -> PyResult<()> {
7169
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
7270

7371
Ok(())
7472
}
7573
```
7674

77-
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+
```
7883

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 build and execute the module:
8085

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+
```
82106

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:
85108

86109
```toml
87110
[dependencies.pyo3]
@@ -116,7 +139,7 @@ fn main_(py: Python) -> PyResult<()> {
116139
}
117140
```
118141

119-
Our guide has [a section](https://pyo3.rs/latest/python_from_rust.html) with lots of examples
142+
The guide has [a section](https://pyo3.rs/latest/python_from_rust.html) with lots of examples
120143
about this topic.
121144

122145
## Tools and libraries

guide/src/building_and_distribution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ You can also symlink (or copy) and rename the shared library from the `target` f
4646
- on Windows, rename `libyour_module.dll` to `your_module.pyd`.
4747
- on Linux, rename `libyour_module.so` to `your_module.so`.
4848

49-
You can then open a Python shell in the same folder and you'll be able to use `import your_module`.
49+
You can then open a Python shell in the same folder and you'll be able to run `import your_module`.
5050

5151
## `Py_LIMITED_API`/`abi3`
5252

0 commit comments

Comments
 (0)