Why PyInstaller misses dynamic imports and how hidden imports fix the gap
PyInstaller bundles Python apps into standalone executables by statically scanning source files for import statements, without executing any code. This means modules imported dynamically at runtime — such as those loaded via importlib.import_module() with variable names, or discovered through plugin-scanning utilities — never appear in the dependency graph and are silently omitted. The resulting frozen executable can run fine initially but crash deep in specific code paths with a ModuleNotFoundError, making the bug hard to catch post-build. Developers can address this by explicitly listing suspected dynamic imports in a 'hidden imports' configuration, a feature PyInstaller provides for exactly this scenario. A practical rule of thumb is to declare any module referenced only inside function bodies or through runtime logic, since static analysis cannot reliably detect these dependencies.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in