Skip to content
Shop

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:

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.

bash
yarn run tauri-app
cd tauri-python

Install Dependencies

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

python
print("Hello from Python sidecar!", flush=True)

Run the following to bundle the application.

bash
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

python
print("Hello from Python sidecar!", flush=True)
json
{
  ...
  "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"
    ]
  }
}
json
{
  "$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"
  ]
}
html
<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>

Resources