Default permissions for Tauri Plugin Python
mainBy default, the plugin includes a set of permissions that allow basic interaction with the Python runtime. The default permission set includes:
allow-call-functionallow-read-variable
repository·main·Indexed 41 days ago
https://github.com/marcomq/tauri-plugin-pythonA plugin for Tauri v2 that allows developers to use Python as a backend. It supports both full CPython via PyO3 for library compatibility (e.g., numpy, pandas) and a lightweight Rust-based interpreter via RustPython for single-binary deployment. The plugin provides a bridge between the Rust-based Tauri core and Python logic, enabling Python functions to be called from the JavaScript frontend.
By default, the plugin includes a set of permissions that allow basic interaction with the Python runtime. The default permission set includes:
allow-call-functionallow-read-variableThe plugin supports two backends. Choosing the right one depends on your compatibility and deployment requirements.
numpy, pandas); fast performance.libpython to be available on the target machine; more complex deployment.By default, the plugin is restricted to prevent arbitrary code execution:
runPython command: This command (which allows injecting Python code directly via JavaScript) is disabled by default via permissions.register command: This command (which allows adding new callable functions from JavaScript) is disabled by default.Recommendation: Ensure your user interface is not accessible via a network URL in production to prevent unauthorized code injection.
To run the provided Vanilla JavaScript example, follow these steps in order:
rust, python, and nodeJs installed.cd ../../
npm install
npm run buildcd examples/plain-javascript
npm install
npm run tauri devTo run on iOS:
npx @tauri-apps/cli plugin ios initnpm run tauri ios dev# Base path setup
cd ../../
npm install
npm run build
# Example path setup
cd examples/plain-javascript
npm install
npm run tauri devsrc-python/ resources.When a Python call fails, the plugin returns the error as a rejected promise. You should wrap your calls in a try...catch block to capture the Python error message or traceback.
In development builds (tauri dev), the plugin also prints the full Python traceback to stderr, prefixed with [tauri-plugin-python]. In release builds, errors are not logged to the terminal to prevent leaking information to end users.
try {
await call.greet_python("input value");
} catch (err) {
console.error(err); // contains the Python error message / traceback
}Since PyO3 uses a shared libpython by default, deployment requires providing the library on the target machine.
Recommended Approaches:
.venv folder as a Tauri resource so libpython is bundled with your app.libpython statically into your binary.Example: Static Embedding with PyOxidizer:
pip install pyoxidizer.pyoxidizer generate-python-embedding-artifacts src-tauri/target/pyembed.# src-tauri/.cargo/config.toml
PYO3_CONFIG_FILE = { value = "target/pyembed/pyo3-build-config-file.txt", relative = true }pyoxidizer generate-python-embedding-artifacts src-tauri/target/pyembed# src-tauri/.cargo/config.toml
PYO3_CONFIG_FILE = { value = "target/pyembed/pyo3-build-config-file.txt", relative = true }If you are not using the provided template, follow these steps to integrate the Python plugin into your Tauri project:
tauri-plugin-python to your Cargo.toml.tauri-plugin-python-api to your package.json.src-tauri/capabilities/default.json, update the permissions array to include "python:default".src-tauri/src-python/main.py (e.g., def greet_python(...):).src-tauri/src/lib.rs, initialize the plugin with your exported functions:.plugin(tauri_plugin_python::init(["greet_python"]))index.html:<script type="module" src="/tauri-plugin-python-api/index.iife.js" defer></script>registerJs("greet_python");call object:call.greet_python(...args);// src-tauri/src/lib.rs
.plugin(tauri_plugin_python::init(["greet_python"]))<!-- index.html -->
<script type="module" src="/tauri-plugin-python-api/index.iife.js" defer></script>// JavaScript usage
registerJs("greet_python");
call.greet_python(...args);To quickly integrate Python into an existing Tauri v2 application using the default PyO3 backend, follow these steps:
npm run tauri add python.src-tauri/src-python/main.py. You must define the _tauri_plugin_functions list to make functions callable from the UI.tauri.conf.json resources so it is included in the production build.callFunction from tauri-plugin-python-api to invoke your Python functions.npm run tauri add python# src-tauri/src-python/main.py
_tauri_plugin_functions = ["greet_python"]
def greet_python(name):
return f"Hello {name} from Python"// tauri.conf.json
"bundle": { "resources": ["src-python/"] }import { callFunction } from 'tauri-plugin-python-api'
console.log(await callFunction("greet_python", ["world"])) // "Hello world from Python"The file src-python/main.py is required. All Python files must be included in the Tauri resource configuration (tauri.conf.json). Your bundled resource layout must mirror your local layout so that Python imports resolve correctly.
You can define resources as a list or a source→target map in tauri.conf.json:
"bundle": { "resources": ["src-python/"] }"bundle": {
"resources": ["src-python/"]
}PyO3 requires a libpython to be available at runtime. The plugin does not have a setting to point to a Python installation; instead, the OS dynamic loader finds libpython via standard search paths (rpath, LD_LIBRARY_PATH, etc.) based on how the plugin was linked at build time.
otool -L on macOS or ldd on Linux) and bundle that library next to your executable.python-build-standalone) at build time via PYO3_CONFIG_FILE, then bundle that Python as a resource.Note: Bundling a venv (src-python/.venv) only adds site-packages to sys.path; it does not provide libpython.
If you prefer not to use the tauri add command, you can install the plugin manually:
$ cargo add tauri-plugin-python.$ npm install tauri-plugin-python-api."python:default" to the permissions array in src-tauri/capabilities/default.json.src-tauri/src/lib.rs, add .plugin(tauri_plugin_python::init_and_register(vec!["your_function_name"])) to your tauri::Builder.import { call, registerJs } from 'tauri-plugin-python-api'.window.__TAURI__.python.// src-tauri/src/lib.rs
tauri::Builder::default()
.plugin(tauri_plugin_python::init_and_register(vec!["greet_python"]))import { call, registerJs } from 'tauri-plugin-python-api'
registerJs("greet_python");
console.log(await call.greet_python("input value"));