Multiple Hidden Imports in Pyinstaller
Asked Answered
D

2

7

I know how to use the hidden import function in pyinstaller to import one python library. What if I have multiple python libraries like pyautogui and pandas? How do I hidden import them both? I’m thinking it looks something like this:

pyinstaller Pythonmain.py —hidden-import=‘pyautogui, pandas’

I’m sure it’s simple but I can’t find the answer anywhere.

Dianoia answered 23/5, 2021 at 15:7 Comment(0)
G
16

You can repeat the --hidden-import option:

pyinstaller main.py -—hidden-import pandas --hidden-import pyautogui

From the documentation, emphasis mine:

--hidden-import MODULENAME

Name an import not visible in the code of the script(s). This option can be used multiple times.

Gorton answered 23/5, 2021 at 15:13 Comment(5)
Thanks! I’ll test it soon, it should work!Dianoia
Is there a way to specify files through regex for example? As to not repeat a lot of importsAruwimi
@Aruwimi you mean the module names right? then, no afaik.Hypnos
According to this github.com/pyinstaller/pyinstaller/issues/4588 you could maybe write some code to collect all imports but it's not really simple, best I could find.Aruwimi
thanks @Barbaldo. perhaps some command line manipulation could help here, i'll try to attempt thatHypnos
S
0

If you want to use a bunch of hidden imports it's efficient to use in spec file. on top of your spec file's content, make a list of required modules in something like all_libraries, then define the hidden imports like the following example -

from PyInstaller.utils.hooks import collect_submodules

all_libraries = [
    "altgraph",
    "certifi",
    "charset-normalizer",
    "et-xmlfile",
    "idna",
    "Jinja2",
    "macholib",
    "packaging",
    "pandas",
    "pyparsing",
    "PySide6",
    "PySide6_Addons",
    "PySide6_Essentials",
    "python-dateutil",
    "pytz",
    "setuptools",
    "shiboken6",
    "six",
    "tzdata",
    "urllib3"
]

hidden_imports = []

for l in all_libraries:
    hidden_imports += collect_submodules(l)

a = Analysis(
    ['main.py'],
    pathex=[],
    binaries=[],
    datas=[('./resources/data', 'resources/data'), ('./resources/icons', 'resources/icons')],
    hiddenimports=hidden_imports,
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    noarchive=False,
    optimize=0,
)
pyz = PYZ(a.pure)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.datas,
    [],
    name='main',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=False,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
    icon=['resources/icons/icon.ico'],
)

coll = COLLECT(
    exe,
    a.binaries,
    a.zipfiles,
    strip=False,
    upx=True,
    upx_exclude=[],
    name='main',
)

app = BUNDLE(
    coll,
    name='main.app',
    icon='./resources/icons/icon.ico',
    bundle_identifier=None,
)

Sync answered 22/4 at 13:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.