如何将含图片的.py脚本与.ui文件转成.exe?打包后无法启动
Hey, I’ve run into this exact problem countless times while wrapping Python GUI apps with images, .ui files, and other resources into a single EXE using PyInstaller. Nine times out of ten, it’s a broken resource path issue—when you build a single EXE, PyInstaller extracts your assets to a temporary system folder at runtime, so the relative paths you used in your original code can’t find the files anymore. Let’s walk through the fixes:
1. First, Diagnose the Exact Error
Before diving into code changes, let’s figure out what’s actually breaking. Here’s how:
- Open Command Prompt (CMD) and navigate to the folder where your EXE is located
- Run the EXE directly from the command line, like:
my_app.exe - The console will spit out a specific error message (e.g., "FileNotFoundError: No such file or directory: 'logo.png'")—this tells you exactly which resource is missing.
2. Fix Resource Paths (The Core Solution)
You need to dynamically fetch the correct path to your resources, whether the app is running as raw Python code or as a bundled EXE.
Step 1: Organize Your Resources
First, move all external assets (images, .ui files, etc.) into a dedicated folder, say resources/, in your project root. Your structure should look like this:
my_project/ ├─ main.py ├─ resources/ │ ├─ app_icon.png │ ├─ main_window.ui │ └─ background.jpg
Step 2: Add a Path Helper Function
In your main script (e.g., main.py), add this small function to handle path resolution:
import os import sys def get_resource_path(relative_path): """Get absolute path to resource, works for dev and for PyInstaller""" try: # PyInstaller creates a temp folder and stores path in _MEIPASS base_path = sys._MEIPASS except Exception: # If not bundled, use current working directory base_path = os.path.abspath(".") return os.path.join(base_path, relative_path)
Step 3: Update All Resource References
Replace every hardcoded relative path in your code with calls to this helper. For example:
- Before (broken):
ui_file = "resources/main_window.ui" app_icon = "resources/app_icon.png" - After (fixed):
ui_file = get_resource_path("resources/main_window.ui") app_icon = get_resource_path("resources/app_icon.png")
3. Use the Correct PyInstaller Command
You need to tell PyInstaller to include your resources/ folder when building the EXE. Use the --add-data parameter (note: Windows uses ; to separate source/dest paths, macOS/Linux use :).
Here’s the full command for Windows:
pyinstaller --onefile --windowed --add-data "resources/*;resources/" main.py
Let’s break down the parameters:
--onefile: Packages everything into a single EXE file--windowed: Hides the console window (remove this if you still need to debug via command line output)--add-data: Copies all files inresources/to a matchingresources/folder inside the bundled EXE’s temp directory
4. Additional Troubleshooting Tips
If it’s still not working, check these common pitfalls:
- If you’re loading .ui files with
uic.loadUi(), double-check that you’re using the path returned byget_resource_path()(don’t hardcode here either) - Verify that your image formats are supported (stick to PNG/JPG for broad compatibility)
- If you’re using third-party libraries, PyInstaller might miss some dependencies—use
--hidden-import <library_name>to manually include them (e.g.,--hidden-import PyQt5.QtXml)
内容的提问来源于stack exchange,提问作者Mohammad Hussam Alzeyyat

