Build standalone binary¶
pytauri can be distributed as a Python wheel or compiled into a standalone executable (a regular Tauri application).
Unlike packaging with pyinstaller
after building as a wheel, compiling pytauri into an executable allows you to enjoy all the benefits brought by tauri-cli
.
Get portable Python¶
We will bundle python-build-standalone as a portable Python for distribution.
Please download the Python version you need. Usually, you will use these versions:
cpython-*-x86_64-pc-windows-msvc-install_only_stripped.tar.gz
cpython-*-x86_64-unknown-linux-gnu-install_only_stripped.tar.gz
cpython-*-x86_64-apple-darwin-install_only_stripped.tar.gz
Extract it to src-tauri/pyembed
, make sure the file layout is as follows:
Tell tauri-cli
to ignore it during tauri dev
:
Tip
If you are using an IDE based on pyright
/pylance
, please create a pyproject.toml
file in the root directory of your project (not src-tauri/pyproject.toml
) and add the following configuration to tell pyright
not to analyze src-tauri/pyembed
, as it will consume a large amount of memory:
[tool.pyright]
# see: <https://github.com/microsoft/pyright/blob/1.1.391/docs/configuration.md#environment-options>
exclude = [
"**/node_modules",
"**/__pycache__",
# 👇 necessary, because when `tauri-cli bundles python,
# it will copy `pyembed` to the target directory (i.e., rust output dir).
"**/target",
# 👆
"**/dist",
"**/.venv",
"**/.*",
"src-tauri/pyembed/",
"src-tauri/frontend/",
]
Install your project into the embedded Python environment¶
Warning
Unlike editable install
during development, you need to reinstall your project every time you modify the Python code.
Configure tauri-cli
¶
ref: https://tauri.app/reference/config/#bundle
Create following tauri-cli
configuration file:
{
"bundle": {
"active": true,
"targets": "all",
"resources": {
"pyembed/python": "./"
}
}
}
ref: https://doc.rust-lang.org/cargo/reference/profiles.html
Add the following configuration to Cargo.toml
:
# ...
[profile.bundle-dev]
inherits = "dev"
[profile.bundle-release]
inherits = "release"
Build and bundle¶
ref: https://pyo3.rs/v0.23.3/building-and-distribution.html#configuring-the-python-version
Indicate pyo3 to use the embedded Python interpreter through environment variables, so it does not mistakenly use the system Python interpreter.
Configure RUSTFLAGS
:
Nothing you need to do. Only unix need to set RUSTFLAGS
.
- There is currently an issue with the
sysconfig
ofpython-build-standalone
, which causespyo3
to fail to automatically findlibpython3
during compilation, so we need to set it manually. - We use tauri's
resource_dir
to bundle the portable Python, so we need to setrpath
to tell our binary how to find the bundledlibpython3
at runtime.
Finally, use tauri-cli
to bundle:
Warning
DO NOT set bundle.resources
in tauri.conf.json
directly.
The tauri-cli
will copy bundle.resources
to target/release(debug)
, which is in the same location as your executable.
This will incorrectly cause the copied Python environment to be the Python environment linked at runtime during tauri dev
.
However, during development, you should use a venv
virtual environment.
By using --profile bundle-release
, we ensure that target/release(debug)
is not affected, allowing you to use tauri dev
normally.