I am trying to run an exe file generated by PyInstaller, but I am getting the following error:

642 Views Asked by At

I am creating an executable using pyinstaller. My project includes multiple python files, and I am trying to create a single executable that runs the main.py file. I am working with python snap7 to read data from PLC (https://sourceforge.net/projects/snap7/). There seems to be an issue with the snap7.dll file. When using pyinstaller --onefile main.py, the executable is created no problem. However, when going into the dist folder to test the exe I get this:

Traceback (most recent call last):
  File "PyInstaller\loader\pyimod03_ctypes.py", line 77, in __init__
  File "ctypes\__init__.py", line 376, in __init__
OSError: [WinError 193] %1 is not a valid Win32 application

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "main.py", line 5, in <module>
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "utilities.py", line 9, in <module>
  File "snap7\client\__init__.py", line 64, in __init__
  File "snap7\common.py", line 72, in load_library
  File "snap7\common.py", line 64, in __init__
  File "ctypes\__init__.py", line 454, in LoadLibrary
  File "PyInstaller\loader\pyimod03_ctypes.py", line 79, in __init__
pyimod03_ctypes.install.<locals>.PyInstallerImportError: Failed to load dynlib/dll 'C:\\Users\\***\\AppData\\Local\\Temp\\_MEI265202\\snap7.dll'. Most likely this dynlib/dll was not found when the application was frozen.
[24948] Failed to execute script 'main' due to unhandled exception!

I am unsure the cause of this issue but am stuck in solving it. Some insight is much appreciated, thank you.

1

There are 1 best solutions below

1
MG7702 On

Doing pyinstaller --onefile --add-binary "Path to .dll file;." main.py fixed the error for me