Eesh Gharat
Eesh Gharat

Reputation: 11

My python code works fine but gives exception after pyinstaller

Traceback (most recent call last):
  File "Ish.py", line 1, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 378, in exec_module
  File "ReportGenerator.py", line 1, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 378, in exec_module
  File "fpdf\__init__.py", line 22, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 378, in exec_module
  File "fpdf\fpdf.py", line 97, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 378, in exec_module
  File "fpdf\image_parsing.py", line 25, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 378, in exec_module
  File "fpdf\svg.py", line 38, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 378, in exec_module
  File "fpdf\output.py", line 35, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 378, in exec_module
  File "fontTools\subset\__init__.py", line 8, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 378, in exec_module
  File "fontTools\ttLib\tables\otTables.py", line 19, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 378, in exec_module
  File "fontTools\pens\boundsPen.py", line 2, in <module>
ModuleNotFoundError: No module named 'fontTools.misc.bezierTools'


a = Analysis(
    ['Ish.py'],
    pathex=[],
    binaries=[],
    datas=[('logo.ico', '.')],
    hiddenimports=['fontTools.misc.bezierTools', 'fontTools.subset', 'fontTools.ttLib', 'fontTools.pens.boundsPen'],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    noarchive=False,
    optimize=0,
)
pyz = PYZ(a.pure)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.datas,
    [],
    name='Threat Intellisense',
    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=['logo.ico'],
)

The python code works fine. The exe works the first time and when it restarts, it gives this exception and crashes. I restart he application after it has successfully generated a report so that user can give next file.

I also tried --add-data fonttools (entire address) but it did not work.

Upvotes: 0

Views: 50

Answers (1)

Lucas Cimon
Lucas Cimon

Reputation: 2053

This is mostly an issue with PyInstaller usage.

Quoting PyInstaller documentation: https://pyinstaller.org/en/stable/operating-mode.html

PyInstaller finds all the import statements in your script. It finds the imported modules and looks in them for import statements, and so on recursively, until it has a complete list of modules your script may use.

Some Python scripts import modules in ways that PyInstaller cannot detect: for example, by using the __import__() function with variable data, using importlib.import_module(), or manipulating the sys.path value at run time.

A solution in that situation is simply to pass --hidden-import fpdf to PyInstaller.

Note that in your case, the library is fpdf2 but the import is fpdf, meaning you must provide fpdf as a hidden import to PyInstaller. Then PyInstaller will check in the current environment how to resolve this impport, and find the fpdf2 library.

Upvotes: 0

Related Questions