Python using Cython
While the PyInstaller approach is a convenient solution for packaging a Python app as an executable it doesn’t offer a library alternative. This means that your Python code always gets executed as a separate process, with all the performance and inter-process communication issues that such an approach comes with.
To improve upon this we can leverage Cython instead to compile our Python code as a C library. This is not a plug-and-play sort of solution, if your project was not developed with Cython in mind you will likely have to refactor it quite a bit, but the end result will be much better.
-
projects-dir $ pnpm create tauri-app --beta --mobilecd tauri-app# Always try building before you get too farpnpm installpnpm tauri dev -
We’re going to be using Poetry in this lab but the instructions are easily adaptable for non-Poetry projects. In this lab we’ll develop it in a
src-pyfolder next tosrc-tauri. For simple projects this structure will work just fine.Why should I use Poetry?
Besides simplified management of dependencies and your virtual environment Poetry offers you a lockfile mechanism that vanilla Python doesn’t which helps protect your project against supply chain attacks.
tauri-app $ mkdir src-py && cd src-pypoetry init --name my_tauri_appsrc-py/pyproject.toml [tool.poetry]name = "my_tauri_app"version = "0.1.0"description = ""authors = []license = "MIT"readme = "README.md"[tool.poetry.dependencies]python = "^3.12"[build-system]requires = ["poetry-core"]build-backend = "poetry.core.masonry.api" -
Add Cython
This will add Cython to your
pyproject.tomland install it.tauri-app/src-py $ poetry add cython -
Write a
commands.pyxscriptIn this lab we’re just going to make a basic
greetfunction which takes anameas input and returns a friendly message.That’s not Python!
If the syntax looks scary it’s because we’re using Cython. You can cythonize normal Python scripts but you’ll likely run into FFI related issues. This lab won’t go into how Cython and its syntax works.src-py/commands.pyx cdef extern char* greet(const char* name):return b"Hello " + name + b" from Cython!" -
Write a
setup.pyscriptThis isn’t the only way to do it but it’s the way we’re going for in this lab.
src-py/setup.py from setuptools import setupfrom Cython.Build import cythonizefrom setuptools.extension import Extensionextensions = [Extension("my_tauri_app",["commands.pyx"],library_dirs=[],libraries=[],extra_compile_args=[],extra_link_args=[])]setup(name="my_tauri_app",ext_modules=cythonize(extensions),) -
Compile your Python backend
This will use Cython to compile your
commands.pyxfile into a shared library that we can make use of in our Tauri app.tauri-app/src-py $ poetry run python setup.py build_ext --inplace -
Add your library as a resource
-
Load the shared library in Rust
-
Update the
beforeBuildCommandTo simplify the building process make sure you update the
beforeBuildCommandto include the command for building the Python project. -
Build your app
© 2024 Tauri Contributors. CC-BY / MIT