Run Python from Tauri
April 23, 2025
You will learn how to run python code within a tauri application using a python sidecar.
What you will need:
- Python 3 - The Python programming language
- Tauri v2 - Version 2 of Tauri
- Pyinstaller - bundle a Python application and all its dependencies into a single package
- Tauri Shell plugin - Access the system shell. Allows you to spawn child processes.
- Tauri cli plugin - command line interface access in tauri
Create a new Tauri App
Create a new tauri app and give it a name. I named my app tauri-python
. Once it is created, change directories into the folder for the app.
yarn run tauri-app
cd tauri-python
Install Dependencies
yarn
yarn run tauri add cli
yarn run tauri add shell
yarn tauri dev
Folder structure
The general folder structure of the files created/edited.
tauri-python/
src-tauri/
capabilities/
default.json
binaries/
main-x86_64-apple-darwin
tauri.conf.json
dist/
main
main.py
The Steps
Create your python file (main.py
) in root of tauri project and add the following code to it.
print("Hello from Python sidecar!", flush=True)
Run the following to bundle the application.
pyinstaller --onefile main.py
Once complete a /dist
folder will be created with a file named main
. We will need to rename this file. Type the following command to see your host
name.
rustc -Vv
You should get some information that has something like the following line
host: x86_64-apple-darwin
Rename the main
file in the /dist
folder to main-x86_64-apple-darwin
and move it to the src-tauri/binaries/
folder. You will need to create the binaries
folder if it doesn't exist.
TIP
Please note that you can call the binaries
folder whatever you want. and you must prefix the host with the name of your file (main
).
Add "externalBin": ["binaries/main"]
to the tauri configuration file under the bundle
section and make sure "shell:allow-execute"
is set as a permission in your capabilities file (default.json
).
The Complete Code
print("Hello from Python sidecar!", flush=True)
{
...
"plugins": {
"cli":{}
},
"bundle": {
"active": true,
"targets": "all",
"externalBin": ["binaries/main"],
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/128x128@2x.png",
"icons/icon.icns",
"icons/icon.ico"
]
}
}
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "default",
"description": "Capability for the main window",
"windows": ["main"],
"permissions": [
"core:default",
"opener:default",
{
"identifier": "shell:allow-execute",
"allow": [
{
"name": "binaries/main",
"sidecar": true
}
]
},
"shell:allow-open"
]
}
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<input v-model="name" placeholder="Enter your name" />
<button @click="runSidecar">Talk to Python</button>
<p>Response: {{ response }}</p>
</div>
</template>
<script setup>
import { ref } from 'vue'
import { Command } from '@tauri-apps/plugin-shell';
const msg = 'Vue + Tauri + Python Sidecar'
const name = ref('')
const response = ref('');
const runSidecar = async () => {
const command = Command.sidecar('binaries/main');
const output = await command.execute();
response.value=output.stdout
console.log('Sidecar output:', output.stdout);
};
</script>