Search code examples
pythonpyinstallerglobanalysisbuilt-in

Force PyInstaller to include all built-in modules


We are building an application that dynamically imports other Python and runs it. We don't know ahead of time what modules those other Python files might need, but these other Python files should be able to rely on having all built-in modules available.

Because of how PyInstaller's analysis works, we are losing a lot of built-in modules if our main application isn't using them.

Is there a way to force PyInstaller to include everything in Lib (not site-packages)?

Note: we have considered building up a list of hidden imports, but:

  1. this would need to be done programmatically so that we don't need to manually rebuild this if we change Python versions (and thus change the built-in modules list)
  2. this just "feels" like working around the Analysis step instead of removing it, and I'm a little worried about what side effects that might have.

Solution

  • There is third-party module stdlib_list that has all the modules in the Lib. It also includes built-in modules. Use it from the PyInstaller's hook for main/any module of your app:

    import stdlib_list
    hiddenimports = list(stdlib_list.stdlib_list())
    

    My research shows that there is no built-in way to list standard modules from polluted environment. I suppose that's the reason for PyInstaller no having an easy option to include Standard Lib.