PyInstaller模块依赖路径与可执行文件图标设置问题求助
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.pyto 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.icois 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
--onedirmode: Sometimes--onefilecan 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

