| Crates.io | tauri-plugin-python |
| lib.rs | tauri-plugin-python |
| version | 0.3.6 |
| created_at | 2025-01-04 21:14:03.92108+00 |
| updated_at | 2025-04-01 20:02:09.478111+00 |
| description | A tauri 2 plugin to use python code in the backend. |
| homepage | https://github.com/marcomq/tauri-plugin-python |
| repository | https://github.com/marcomq/tauri-plugin-python |
| max_upload_size | |
| id | 1504249 |
| size | 210,237 |
This tauri v2 plugin is supposed to make it easy to use Python as backend code. It uses RustPython or alternatively PyO3 as interpreter to call python from rust.
RustPython doesn't require python to be installed on the target platform and makes it therefore easy to deploy your production binary. Unfortunately, it has some compatibility issues and is slower than PyO3/CPython. PyO3 is also supported as optional Cargo feature for desktop applications. PyO3 uses CPython as interpreter and therefore has a wide compatibility for available python libraries. It isn't used as default as it requires to make libpython available for the target platform, which can be complicated, especially for mobile targets.
The plugin reads by default the file src-tauri/src-python/main.py during
startup and runs it immediately. Make sure to add all your python source as tauri resource,
so it is shipped together with your production binaries. Python functions are all registered during plugin initialization
and can get called during application workflow.
| Platform | Supported |
|---|---|
| Linux | ✓ |
| Windows | ✓ |
| MacOS | ✓ |
| Android | x* |
| iOS | ✓* |
x* There is currently a known issue on tauri+android that prevents reading files.
https://github.com/tauri-apps/tauri/issues/11823
So python code cannot be read on android right now. Android is going to be supported as soon as reading resource files will be fixed.
✓* Linux, Windows and MacOS support PyO3 and RustPython as interpreter. Android and IOS
currently only support RustPython.
Android and iOS might also be able to run with PyO3 in theory but would require to have CPython
to be compiled for the target platform. I still need to figure out how to
cross compile python and PyO3 for iOS and Android. Ping me if you know how to do that.
You can use this plugin for fast prototypes or for production code. It might be possible that you want to use some python library or code that is not available for rust yet. In case that you want to ship production software packages, you need to make sure to also ship all your python code. If you use PyO3, you also need to ship libpython too.
# src-tauri/Cargo.toml
tauri-plugin-python = { version="0.3", , features = ["pyo3"] }
There is a sample Desktop application for Windows/Linux/MacOS using this plugin and vanilla Javascript in examples/plain-javascript.
These steps assume that you already have a basic tauri application available. Alternatively, you can immediately start with the application in "example" directory.
npm run tauri add pythonsrc-tauri/src-python/main.py and modify it according to your needs, for example add# src-tauri/src-python/main.py
_tauri_plugin_functions = ["greet_python"] # make "greet_python" callable from UI
def greet_python(rust_var)
return str(rust_var) + " from python"
"bundle": {"resources": [ "src-python/**/*"], to tauri.conf.json so that python files are bundled with your applicationimport { callFunction } from 'tauri-plugin-python-api'outputEl.textContent = await callFunction("greet_python", [value]) to get the output of the python function greet_python with parameter of js variable valueCheck the examples for alternative function calls and code sugar.
Tauri events and calling js from python is currently not supported yet. You would need to use rust for that.
$ cargo add tauri-plugin-python$ npm install tauri-plugin-python-apipermissions:[] in src-tauri/capabilities/default.json and add "python:default"src-tauri/src-python/main.py and add python code, for example:# src-tauri/src-python/main.py
def greet_python(rust_var)
return str(rust_var) + " from python"
.plugin(tauri_plugin_python::init_and_register(vec!["greet_python")) to tauri::Builder::default(), usually in src-tauri/src/lib.rs. This will initialize the plugin and make the python function "greet_python" available from javascript.window.__TAURI__.python. For modern javascript:import { callFunction } from 'tauri-plugin-python-api'
console.log(await callFunction("greet_python", ["input value"]))
-> this will call the python function "greet_python" with parameter "input value". Of course, you can just pass in any available javascript value. This should work with "boolean", "integer", "double", "string", "string[]", "double[]" parameter types.
Alternatively, to have more readable code:
import { call, registerJs } from 'tauri-plugin-python-api'
registerJs("greet_python");
console.log(await call.greet_python("input value"));
You either need to have python installed on the target machine or ship the shared python library with your package. You also may link the python library statically - PyO3 may do this by default if it finds a static python library. In addition, you need to copy the python files so that python files are next to the binary.
The file src-python/main.py is required for the plugin to work correctly.
You may also add additional python files or use a venv environment.
The included resources can be configurable in the tauri.conf.json file.
Check the tauri and PyO3 documentation for additional info.
By default, this plugin cannot call arbitrary python code. Python functions can only be called if registered from rust during plugin initialization. It may still be possible to read values from python. This can be prevented via additional tauri permissions.
Keep in mind that this plugin could make it possible to run arbitrary python code when using all allow permissions. It is therefore highly recommended to make sure the user interface is not accessible by a network URL in production.
The "runPython" command is disabled by default via permissions. If enabled, it is possible to inject python code directly via javascript. Also, the function "register" is disabled by default. If enabled, it can add control from javascript which functions can be called. This avoids to modify rust code when changing or adding python code. Both functions can be enabled during development for rapid prototyping.
If already know that you just want to develop completely in python, you might want to take a look at pytauri. It is a different approach to have all tauri functionality completely in python.
This approach here with tauri-plugin-python is more lightweight and it is for you, if you