Skip to content

Commit e71c134

Browse files
committed
update readme and example
1 parent 8f4f408 commit e71c134

File tree

4 files changed

+65
-15
lines changed

4 files changed

+65
-15
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ tauri = { version = "2" }
1717
serde = { version = "1", features = ["derive"] }
1818
thiserror = "2"
1919
lazy_static = "1.5.0"
20-
pyo3 = { version = "0.23.3", features=["auto-initialize", "abi3-py39", "generate-import-lib"], optional = true }
20+
pyo3 = { version = "0.23.3", features=["auto-initialize", "generate-import-lib"], optional = true }
2121
rustpython-pylib = { version = "0.4.0" }
2222
rustpython-stdlib = { version = "0.4.0", features = ["threading"] }
2323
rustpython-vm = { version = "0.4.0", features = [

README.md

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ This [tauri](https://v2.tauri.app/) v2 plugin is supposed to make it easy to use
44
It uses [RustPython](https://github.com/RustPython/RustPython) or alternatively [PyO3](https://pyo3.rs) as interpreter to call python from rust.
55

66
RustPython doesn't require python to be installed on the target platform and makes it
7-
therefore easy to deploy your production binary. Unfortunately, it has some
8-
compatibility issues and is slower than PyO3/CPython. PyO3 is also supported as optional Cargo feature for desktop applications.
9-
PyO3 uses CPython as interpreter and therefore has a wide compatibility for available python libraries.
7+
therefore easy to deploy your production binary. Unfortunately, it doesn't even support
8+
some usually built-int python libraries and is slower than PyO3/CPython.
9+
PyO3 is supported as optional Cargo feature for desktop applications.
10+
PyO3 uses the usual CPython as interpreter and therefore has a wide compatibility for available python libraries.
1011
It isn't used as default as it requires to make libpython available for the target platform,
1112
which can be complicated, especially for mobile targets.
1213

@@ -33,18 +34,39 @@ Android and iOS might also be able to run with PyO3 in theory but would require
3334
to be compiled for the target platform. I still need to figure out how to
3435
cross compile python and PyO3 for iOS and Android. Ping me if you know how to do that.
3536

36-
You can use this plugin for fast prototypes or for production code.
37+
You can use this plugin for fast prototypes or for (early) production code.
3738
It might be possible that you want to use some python library or code that
3839
is not available for rust yet.
3940
In case that you want to ship production software packages, you need
4041
to make sure to also ship all your python code. If you use PyO3, you also need to ship libpython too.
4142

4243
### Switch from RustPython to PyO3
43-
44+
Using [PyO3](https://github.com/PyO3/pyo3) will support much more python libraries than RustPython as it is using CPython.
4445
```toml
4546
# src-tauri/Cargo.toml
4647
tauri-plugin-python = { version="0.3", features = ["pyo3"] }
4748
```
49+
Unfortunately, using PyO3 will use a shared libpython by default, which makes
50+
local development easy but makes
51+
deployment of releases more complicated.
52+
Therefore, it may be recommended to either use [pyoxidizer](https://github.com/indygreg/PyOxidizer) to embed libpython statically
53+
or try to ship the dynamic libpython together with your application, for example as part
54+
of the .venv. Check out the [PyO3 documentation](https://pyo3.rs/v0.24.2/building-and-distribution.html) for additional support.
55+
56+
Example of how to embed libpython statically using PyOxidizer:
57+
58+
This has just been tested locally on MacOS. It may be possible that this is more complicated and requires additional steps on your environment.
59+
60+
Install pyoxidizer `pip install pyoxidizer` in a venv and run it on bash:
61+
```bash
62+
pyoxidizer generate-python-embedding-artifacts src-tauri/target/pyembed
63+
```
64+
Then, add it to your cargo config:
65+
```toml
66+
# src-tauri/.cargo/config.toml
67+
PYO3_CONFIG_FILE = { value = "target/pyembed/pyo3-build-config-file.txt", relative = true }
68+
```
69+
You can check if the release binary has some shared libpython references by running `otool -L tauri_app` on MacOs or `ldd tauri_app` on linux.
4870

4971
## Example app
5072

@@ -63,7 +85,7 @@ _tauri_plugin_functions = ["greet_python"] # make "greet_python" callable from
6385
def greet_python(rust_var):
6486
return str(rust_var) + " from python"
6587
```
66-
- add `"bundle": {"resources": [ "src-python/**/*"],` to `tauri.conf.json` so that python files are bundled with your application
88+
- add `"bundle": {"resources": [ "src-python/"],` to `tauri.conf.json` so that python files are bundled with your application
6789
- add the plugin in your js, so
6890
- add `import { callFunction } from 'tauri-plugin-python-api'`
6991
- add `outputEl.textContent = await callFunction("greet_python", [value])` to get the output of the python function `greet_python` with parameter of js variable `value`
@@ -97,16 +119,41 @@ import { call, registerJs } from 'tauri-plugin-python-api'
97119
registerJs("greet_python");
98120
console.log(await call.greet_python("input value"));
99121
```
122+
## Using a venv
123+
124+
Using a python venv is highly recommended when using pip dependencies.
125+
It will be loaded automatically, if the folder is called `.venv`.
126+
It would be recommended to create it in the project root:
127+
```sh
128+
python3 -m venv .venv
129+
source .venv/bin/activate # or run the .venv/bin/activate.bat script
130+
pip install <your_lib>
131+
```
132+
133+
You need to make sure that the relevant venv folders `include` and `lib` are
134+
copied next to the `src-python` tauri resource folder:
135+
136+
`tauri.conf.json`
137+
```json
138+
"resources": {
139+
"src-python/": "src-python/",
140+
"../.venv/include/": "src-python/.venv/include/",
141+
"../.venv/lib/": "src-python/.venv/lib/"
142+
}
143+
```
100144

101145
## Deployment
102146

103-
You either need to have python installed on the target machine or ship the shared
104-
python library with your package. You also may link the python library statically - PyO3
105-
may do this by default if it finds a static python library. In addition, you need
106-
to copy the python files so that python files are next to the binary.
147+
The file `src-python/main.py` is always required for the plugin to work correctly.
148+
The included resources can be configured in the `tauri.conf.json` file.
149+
You need to make sure that all python files are included in the tauri resource files and that
150+
your resource file structure is similar to the local python file structure.
151+
152+
There are no other extra steps required for **RustPython** as it will be linked statically.
153+
For **PyO3**, you either need to have python installed on the target machine or ship the shared
154+
python library with your package. You also may link the python library statically, for example by using PyOxidizer. In addition, you need
155+
to copy all additional python files so that python files are next to the binary and should also export the .venv folder, if you are using a venv.
107156

108-
The file `src-python/main.py` is required for the plugin to work correctly.
109-
You may also add additional python files or use a venv environment.
110157
The included resources can be configurable in the `tauri.conf.json` file.
111158

112159
Check the tauri and PyO3 documentation for additional info.
@@ -133,5 +180,5 @@ It is a different approach to have all tauri functionality completely in python.
133180
This approach here with tauri-plugin-python is more lightweight and it is for you, if you
134181
- still want to write rust code
135182
- already have a tauri application and just need a specific python library
136-
- just want to simply support rare custom plugins
183+
- just want to simply support rare tauri plugins
137184
- want to embed python code directly in your javascript
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
[env]
3+
PYO3_CONFIG_FILE = { value = "target/pyembed/pyo3-build-config-file.txt", relative = true }

examples/plain-javascript/src-tauri/tauri.conf.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"active": true,
2424
"targets": "all",
2525
"resources": [
26-
"src-python/**/*"
26+
"src-python/"
2727
],
2828
"icon": [
2929
"icons/32x32.png",

0 commit comments

Comments
 (0)