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

PyInstaller模块依赖路径与可执行文件图标设置问题求助

Fixing Two Common PyInstaller Issues: Module Not Found and Icon Not Showing

Hey there, I’ve run into both of these exact issues before—let’s break them down and fix them one by one!

1. Resolving ModuleNotFoundError: No module named 'getDigits'

The core problem here is that PyInstaller’s static code analyzer can’t detect modules imported via dynamic sys.path.insert calls. It doesn’t know to bundle your Functions folder unless you explicitly tell it, or adjust your import structure. Here are two reliable fixes:

Option 1: Restructure Your Imports (Cleanest Long-Term Fix)

Turn your Functions folder into a proper Python package by adding an empty __init__.py file inside it. Then update your import in file.py to:

from Functions import getDigits

PyInstaller will automatically recognize this package and include it in the build without extra flags.

Option 2: Force PyInstaller to Bundle the Folder (No Code Rewrites)

Use the --add-data flag to include the Functions folder in your bundle, and tweak your path logic to work both in development and when frozen:

  • First, update the path setup in file.py to handle the bundled environment:
    import sys
    import os
    
    # Get the correct base path (works for both script and exe)
    if getattr(sys, 'frozen', False):
        # Running as a bundled executable
        base_path = sys._MEIPASS
    else:
        # Running as a regular Python script
        base_path = os.path.dirname(os.path.abspath(__file__))
    
    # Add Functions directory to path using the resolved base path
    sys.path.insert(0, os.path.join(base_path, 'Functions'))
    import getDigits
    
  • Then build with this command (use : instead of ; if you’re on macOS/Linux):
    pyinstaller --onefile --add-data "Functions;Functions" run_files.py
    

This ensures the Functions folder is packaged with your exe, and your code can locate it correctly at runtime.

2. Fixing the Missing Icon Issue

If your icon isn’t showing up after using --icon=folder.ico, try these troubleshooting steps:

  • Validate your ICO file: PyInstaller requires a standard Windows ICO file (supports multiple sizes like 32x32, 64x64). If you converted a PNG to ICO with a low-quality tool, it might not be recognized. Use a dedicated ICO generator (like desktop apps or reputable online tools) to create a valid file.
  • Double-check the icon path: Make sure the path to folder.ico is accurate. If it’s in a subfolder, use the full relative path (e.g., --icon=assets/folder.ico). If the path has spaces, wrap it in quotes:
    pyinstaller --onefile --icon="My Icons/folder.ico" run_files.py
    
  • Refresh Windows icon cache: Windows often caches old icons, so the new one might not show up immediately. Try moving the exe to a different folder, or run this command in Command Prompt to force a refresh:
    ie4uinit.exe -show
    
  • Test with --onedir mode: Sometimes --onefile can cause icon display delays. Build with --onedir (instead of --onefile) to see if the icon appears—if it does, the issue is likely related to caching or --onefile’s temporary extraction process.

内容的提问来源于stack exchange,提问作者YuanL

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 20:12:55