Skip to content

How to Fix Python PyInstaller Hook Error

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about How to Fix Python PyInstaller Hook Error. We cover key concepts, practical examples, and best practices.

PyInstaller produces an executable that crashes with ModuleNotFoundError when dynamic imports are not detected, hooks are missing for certain libraries, or data files are not included.

The Wrong Way

pyinstaller --onefile myscript.py
# myscript.py
import pandas as pd
print(pd.DataFrame({"a": [1]}))

Running the executable:

ModuleNotFoundError: No module named 'pandas._libs.parsers'

The Right Way

pyinstaller --onefile --hidden-import=pandas._libs.parsers myscript.py

Or better, use a spec file:

# myscript.spec
a = Analysis(
    ['myscript.py'],
    pathex=[],
    binaries=[],
    datas=[],
    hiddenimports=['pandas._libs.parsers', 'pandas._libs.tslibs'],
    hookspath=[],
    runtime_hooks=[],
    excludes=[],
)

pyz = PYZ(a.pure)
exe = EXE(pyz, a.scripts, a.binaries, a.zipfiles, a.datas, name='myscript', debug=False, strip=False, upx=True, console=True)

Build:

pyinstaller myscript.spec

The executable runs without import errors.

Prevention Tips

  • Use --hidden-import for modules not detected automatically.
  • Create a .spec file for complex projects.
  • Test the executable on a clean system.
  • Use --debug=imports to see which imports are bundled.
  • Include data files with --add-data "data/*:data".

Common Mistakes with pyinstaller hook

  1. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  2. Misunderstanding that String is [Char] with poor performance for large text operations
  3. Using foldl instead of foldl' causing stack overflow on large lists

These mistakes appear frequently in real-world PYTHON code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between --onefile and --onedir?

--onefile creates a single executable that extracts to a temp directory at runtime. --onedir creates a folder with the executable and support files. --onedir starts faster.

Why does PyInstaller miss some imports?

PyInstaller uses static analysis to find imports. Dynamic imports (importlib.import_module()), __import__(), and import in eval strings are not detected. Add them with --hidden-import.

How do I bundle data files with PyInstaller?

Use --add-data "source:dest" (Linux/macOS) or --add-data "source;dest" (Windows). Access them with sys._MEIPASS at runtime. Use os.path.join(getattr(sys, '_MEIPASS', '.'), 'data').

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro