[Python] Pyinstaller and hiddenimports: how to force to import a package that doesn't get...

Discussão em 'Python' iniciado por Stack, Julho 30, 2025.

  1. Stack

    Stack Membro Participativo

    I tried to generate a .exe file using pyinstaller.

    It works fine, except for fact that one package was not automatically detected and imported by pyinstaller.

    Such package, that in this example we will call "packageOfInterest", did not get imported because the developers did not provide an hook.

    Reading some documentation I understood that this issue could be easily fixed with the following line to be added in the .spec:

    hiddenimports=["packageOfInterest"]


    Unfortunately it doesn't work, the "packageOfInterest" was not imported even using such line of code.

    So my question is: What I'm still missing in the .spec file?

    Below my .spec file that I've been using with success with many applications where the packages could be automatically detected by pyinstaller, therefore this is not the case.

    import sys
    import os

    from kivy_deps import sdl2, glew
    from kivymd import hooks_path as kivymd_hooks_path
    path = os.path.abspath(".")

    a = Analysis(
    ["MyScript.py"],

    # "packageOfInterest" in the "hiddenimports" is the package name
    # that pyinstaller could not import automatically
    hiddenimports=["kivymd.stiffscroll", "packageOfInterest"],

    pathex=[path],
    hookspath=[kivymd_hooks_path],
    datas = [("media\\", "media\\")],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=None,
    noarchive=False,
    )
    pyz = PYZ(a.pure, a.zipped_data, cipher=None)

    exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
    debug=False,
    strip=False,
    upx=True,
    name="MyScript",
    console=True,
    icon='myicon.ico'
    )


    The location of "packageOfInterest" is at the path:

    C:\Users\ASUS\AppData\Local\Programs\Python\Python39\Lib\site-packages


    Maybe this path should be specified somewhere (e.g. should be addedd in the "pathex" after the "path" value).

    In general would be great to identify a clear method that check for all packages imported via "MyScript.py" that cannot automatically be imported by pyinstaller, and that will force their import.

    At the same time would be appreciated to understand how to customized to .spec in order to fix the issue.

    thanks in advance

    Continue reading...

Compartilhe esta Página