0

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.

MG7702
  • 17
  • 3
  • You probably need to manually add the dll file to the compiled binary. – Alexander May 11 '23 at 11:45
  • this is a very common problem which i don't have a general solution but i can express the problem for you. py to exe conversion results in with this problem for some packages exe file can't find these packages when run. i encountered this with keyboard, mediapipe, snap7,lackey etc. Also instead of making the exe 'onefile' you can choose 'onedir' and then copy the not found package folders from your computer and paste it next to your exe file. Yes its a very lame solution. – Barış Aktaş May 11 '23 at 14:09
  • My problem is solved when I removed python and reinstalled version 3.9.0. – Barış Aktaş Aug 17 '23 at 12:57

1 Answers1

0

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

MG7702
  • 17
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 12 '23 at 15:46