You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python 3.6生成带资源与桌面快捷方式的MSI/EXE安装包?

解决方案:Python 3.6 GUI 应用封装为带资源与快捷方式的安装程序

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 .ico file)
  • 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:

  1. Download and install Inno Setup (search for it directly, no external links needed)
  2. 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"
  1. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:21:33