简体   繁体   中英

Pyinstaller and hiddenimports: how to force to import a package that doesn't get automatically imported by pyinstaller

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 (eg 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

The .exe crashed because one file is not showing. Such file was belonging to the "packageofinterest" (mne)

hmm.. you could try use --collect-data packageofinterest as it seems you are missing some files which are belongs to the package.

Also you can use --hidden-import packageofinterest if you don't want to use the spec file.

This question seems old but I hope it helps someone in the future. I recently tried to create a GUI Bundling app. that circumvents as many imported module related issues when I encountered similar interest of forcing PyInstaller to pickup module used by any project I try to Bundle into executable. Here's my workaround.

Every Python module has the file and/or path property/ies. This is because every third-party implementation we install for use (via pip, easy_install, etc) is provided as a module(path via file ) or a package(path via path ). Only those available as a package have access to both file and path properties.

So, let's assume the modules that couldn't be picked up by PyInstaller are zipfile and psutil . The former is a module while the later is a package.

import zipfile  # Use any of the import rules.
print( zipfile.__file__ )  # Take a look at where its absolute path resides.

import psutil
print( psutil.__path__ )
print( psutil.__file__ )  # This will return the package's __init__.py file. Definitely not what you want.

Now, add this path to your PyInstaller's command using the --add-data option. syntax :

pyinstaller --add-data module_absolute_path;destination my_program_startup_file.py

NOTE: -The spaces around the print statements is only for readability sake. -The destination is always a '.' for modules and module name for packages Ex: psutil/ [refer PyInstaller documentation for clarity]

Example :

pyinstaller --onefile --clean --add-data C:\Users\USER\AppData\Local\Programs\Python\Python310\Lib\site-packages\psutil;psutil\ my_program_startup_file.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM