如何将Python脚本打包为树莓派Raspbian可双击运行的可安装应用?
Got it, let's tackle this step by step—you want to turn your Python script into a double-clickable, tamper-proof app on Raspberry Pi, plus make it installable like a native Raspbian app. Here's how to do both:
For a single, double-clickable binary that hides your code from edits, PyInstaller is the go-to tool. It works great on Raspberry Pi's ARM architecture.
First, install PyInstaller. Since Raspberry Pi uses Python 3 by default, run:
pip3 install pyinstaller(If you hit permission issues, add
--userto install it for your user only, or usesudo.)Package your script:
- If your app is a GUI program (no terminal window needed), use:
pyinstaller --onefile --windowed my_app.py - If it's a command-line tool, skip the
--windowedflag:pyinstaller --onefile my_app.py - The
--onefileflag bundles all dependencies into a single file for easy distribution.
- If your app is a GUI program (no terminal window needed), use:
Find your executable:
After packing finishes, head to thedistfolder in your project directory. The file namedmy_app(no file extension) is your standalone executable. Double-click it to run immediately—no Thonny required, and the code is stored as unreadable binary, so no accidental edits.
If you want your app to show up in the Raspberry Pi's application menu and be installable/uninstallable like native apps, turn it into a .deb package. We'll use fpm (Effing Package Management) because it simplifies the process way more than manual Debian config files.
Step 1: Install fpm
First, install its dependencies and the tool itself:
sudo apt update && sudo apt install ruby ruby-dev build-essential sudo gem install fpm
Step 2: Prepare your app's file structure
Create a new folder (e.g., my_app_pkg) and organize your files into the standard Linux directory layout:
usr/bin/: Move the PyInstaller-generated executable here. Give it execute permissions first:chmod +x usr/bin/my_appusr/share/icons/hicolor/48x48/apps/: If you have an app icon, place a 48x48 PNG file (e.g.,my_app.png) here. You can add other sizes (like 64x64) in corresponding subfolders if you want.usr/share/applications/: Create a.desktopfile (e.g.,my-app.desktop) with this content (adjust for your app):[Desktop Entry] Type=Application Name=My Awesome App Exec=/usr/bin/my_app Icon=my_app Comment=A custom app I built for Raspberry Pi Categories=Utility;Education; # Pick relevant categories (e.g., Utility, Games) Terminal=false # Set to true if your app needs a terminal
Step 3: Build the .deb package
Navigate to your my_app_pkg folder and run:
fpm -s dir -t deb -n my-app -v 1.0.0 -a armhf .
Let's break down the flags:
-s dir: Source is a directory structure-t deb: Target is a Debian package-n my-app: Name your package (use lowercase, hyphens instead of spaces)-v 1.0.0: Set your app's version number-a armhf: Specify the architecture (most Raspberry Pi models use armhf; usearm64for Pi 4/5 with 64-bit OS).: Package all contents of the current directory
Step 4: Install and test
You'll find the generated .deb file in your my_app_pkg folder. Install it with:
sudo dpkg -i my-app_1.0.0_armhf.deb
Now your app will appear in the Raspberry Pi's application menu—just click it to launch. You can also run it from the terminal with my-app, and uninstall it later with sudo apt remove my-app.
- If your script uses Raspberry Pi-specific libraries (like
RPi.GPIO) and PyInstaller misses them, use the--add-dataflag to include them manually. For example:pyinstaller --onefile --windowed --add-data "/usr/lib/python3/dist-packages/RPi:/RPi" my_app.py - Skipping
--onefilein PyInstaller will generate a folder instead of a single file—this speeds up launch times but requires distributing the whole folder.
内容的提问来源于stack exchange,提问作者miloshIra

