如何用Python 3.6生成带资源与桌面快捷方式的MSI/EXE安装包?
Hey there, let's tackle this step by step. First off, Python 3.6 does work with PyInstaller—you just need to use a compatible version of PyInstaller. The newer versions dropped support for older Python releases, so that's probably where you ran into issues. Let's break this down into actionable steps:
1. Install Compatible PyInstaller Version
First, uninstall any existing PyInstaller and install a version that supports Python 3.6. PyInstaller 4.0 is the last version that officially supports Python 3.6, so run this command:
pip uninstall pyinstaller -y pip install pyinstaller==4.0
2. Bundle Image Resources Correctly
To make sure your app can access images after packaging, you need to tell PyInstaller to include these files. There are two ways to do this:
Option A: Use Command Line Arguments
If your images are in a folder named assets (e.g., assets/logo.png), run PyInstaller with the --add-data flag. On Windows, the syntax is:
pyinstaller --add-data "assets/*;assets/" --windowed --icon=your_icon.ico your_app.py
--windowed: Hides the console window (ideal for GUI apps)--icon: Sets the app's custom icon (use a.icofile)- The
;separates the source and destination paths (on macOS/Linux, use:instead)
Option B: Use a Spec File (More Flexible)
Generate a spec file first to have full control over packaging:
pyinstaller --name YourAppName --windowed --icon=your_icon.ico your_app.py
Open the generated YourAppName.spec file, find the datas list, and add your image resources explicitly:
datas = [ ('assets/logo.png', 'assets'), ('assets/background.jpg', 'assets'), # Add all your image files here ]
Then rebuild using the spec file:
pyinstaller YourAppName.spec
Critical Note: In your Python code, use this helper function to access resources correctly (works both locally and after packaging):
import os import sys def get_resource_path(relative_path): """ Get absolute path to resource, works for dev and PyInstaller builds """ try: # PyInstaller creates a temp folder and stores path in _MEIPASS base_path = sys._MEIPASS except Exception: base_path = os.path.abspath(".") return os.path.join(base_path, relative_path) # Usage example: logo_path = get_resource_path('assets/logo.png')
3. Create a Desktop Shortcut with Icon
PyInstaller doesn't directly generate desktop shortcuts, but you can use Inno Setup (free, Windows-only) to wrap the PyInstaller-generated folder into a proper installer that creates shortcuts. Here's a quick setup:
- Download and install Inno Setup (search for it directly, no external links needed)
- Create a new script file (e.g.,
installer.iss) with this template, adjusting paths and names to match your app:
[Setup] AppName=Your App Name AppVersion=1.0 DefaultDirName={autopf}\YourAppName DefaultGroupName=YourAppName OutputDir=.\installer_output OutputBaseFilename=YourAppInstaller SetupIconFile=your_icon.ico [Files] Source: "dist\YourAppName\*"; DestDir: "{app}"; Flags: recursesubdirs createallsubdirs [Icons] Name: "{group}\Your App Name"; Filename: "{app}\YourAppName.exe"; IconFilename: "{app}\your_icon.ico" Name: "{commondesktop}\Your App Name"; Filename: "{app}\YourAppName.exe"; IconFilename: "{app}\your_icon.ico"
- Open this script in Inno Setup and click "Compile"—it will generate an installer that creates both start menu and desktop shortcuts with your custom icon, and installs all your app files including images.
4. Verify the Installer
Run the generated installer, install the app, and launch it. Check that:
- The app opens without errors
- All images display correctly
- The desktop shortcut has your custom icon
For macOS or Linux, similar tools exist (like create-dmg for macOS or dpkg-deb for Debian-based systems), but Inno Setup is the most straightforward for Windows, the most common use case for desktop shortcuts.
内容的提问来源于stack exchange,提问作者Sumit G

