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

PyInstaller打包EXE遇openpyxl模块找不到问题求助

Fixing "ModuleNotFoundError: No module named 'openpyxl'" When Packing with PyInstaller (Single EXE)

Hey, I see you've already tried using the official hook for openpyxl but still hit that module not found error. Let's walk through the possible issues and fixes step by step:

1. Update Your Hook File to Collect Both Data and Modules

Your current hook only collects data files for openpyxl, but PyInstaller might be missing the actual module subcomponents. Modify your hook-openpyxl.py to include both data and submodules:

from PyInstaller.utils.hooks import collect_data_files, collect_submodules

datas = collect_data_files('openpyxl')
hiddenimports = collect_submodules('openpyxl')

The collect_submodules function pulls in all of openpyxl's nested modules, which prevents missing dependencies that the basic data collection might miss.

2. Double-Check Your Packing Command's Filename

Wait, your test script is named testHi.py, but your pack command uses hiTest.py—that's a typo! Make sure the filename in your command matches the actual script you're trying to pack. The correct command should be:

pyinstaller.exe --onefile --icon=py.ico --additional-hooks-dir=. testHi.py

If your script is actually named hiTest.py, ignore this, but always confirm the hook file is in the same directory as your target script and --additional-hooks-dir=. points to that directory correctly.

3. Force Hidden Imports Directly in the Command (Quick Test)

If the hook still isn't working, you can bypass it temporarily by specifying hidden imports directly in the pack command to verify if that's the root issue:

pyinstaller.exe --onefile --icon=py.ico --hidden-import=openpyxl --hidden-import=openpyxl.* testHi.py

If the EXE runs correctly after this, it means your hook wasn't configured properly—go back to step 1 to adjust it.

4. Verify Your Python Environment Consistency

Make sure you're using the same Python environment where you installed openpyxl to run PyInstaller. If you use virtual environments, activate it first before packing. If you have multiple Python versions on your system, confirm that pyinstaller.exe belongs to the environment with openpyxl installed.

Quick check to confirm:

pip show openpyxl

Look at the "Location" field in the output—it should match the site-packages directory of the Python you're using to run PyInstaller.

5. Clean PyInstaller Cache Files

Old build cache can sometimes cause weird issues. Delete the build, dist directories, and any generated .spec file before re-packing:

# For Windows
rmdir /s /q build dist
del testHi.spec

# Then re-run the pack command
pyinstaller.exe --onefile --icon=py.ico --additional-hooks-dir=. testHi.py

Go through these steps one by one, and you should get past that openpyxl module error.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 10:12:21