I’m slowly getting confused because I can’t load a module from PyQt5. I create a script that starts a subprocess in which another script is executed. So that I only need one executable file, I load the script into the file with “pyinstaller –onefile –add-data=”.assetsscriptsscript2.py”. So that I have access to the modules from the first script, I pass the values for “sys.path” as parameters to the second script. Unfortunately, this is not executed as expected, but gives me an error that the module cannot be found….
How my scripts are build:
script1.py
main script, converted to executable:
from assets.modules.module1 import Class1
if __name__ == "__main__":
client = Class1("arg1", "arg2")
while True:
# some code here...
# foo
# bar
assets.modules.module1
this module starts the subprocess:
import sys
from subprocess import Popen
# code
# foo
# bar
def resourcePath(resource: str) -> str:
"""
Get the absolute path to a resource.
Works for both development (debug) and PyInstaller (release).
"""
try:
# PyInstaller creates a temp folder and stores the path in `_MEIPASS`
basePath = sys._MEIPASS
except AttributeError:
# Access the path in development mode
basePath = path.abspath(".")
return path.join(basePath, resource).replace("\", "/")
def doSomething(arg1, arg2):
# to read the path when compiled to executable
with open("path/to/sys_path.txt", "w") as file
file.write(str(sys.path))
proc = Popen([resourcePath("assets/executables/pythonw.exe"),
resourcePath("assets/scripts/script2.py"),
arg1,
arg2,
] + sys.path
# foo
# bar
# code
script2.py
script to be executed in subprocess, opens a window:
import sys
if len(sys.argv) > 3:
sys.path = sys.argv[3:]
with open("sys_path.txt", "w") as file:
file.write(str(sys.path))
try:
# ERROR here!
from PyQt5.QtCore import Qt, QTimer, QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
except Exception as e:
with open("error.txt", "w") as file:
file.write("e.msg:n" + e.msg + "nn" + "e.path:n" + e.path)
raise e
"""
Exception has occurred: ImportError
DLL load failed while importing QtCore: The specified module was not found.
File "Projectpathassetsmodulesscript2.py", line 16, in <module>
from PyQt5.QtCore import Qt, QUrl, QTimer
ImportError: DLL load failed while importing QtCore: The specified module was not found.
"""
sys_path.txt
sys.path are both identical:
['C:\Users\USER~1\AppData\Local\Temp\_MEI138122\base_library.zip', 'C:\Users\USER~1\AppData\Local\Temp\_MEI138122\lib-dynload', 'C:\Users\USER~1\AppData\Local\Temp\_MEI138122', 'C:\Users\USER~1\AppData\Local\Temp\_MEI138122\setuptools\_vendor']
['C:\Users\USER~1\AppData\Local\Temp\_MEI138122\base_library.zip', 'C:\Users\USER~1\AppData\Local\Temp\_MEI138122\lib-dynload', 'C:\Users\USER~1\AppData\Local\Temp\_MEI138122', 'C:\Users\USER~1\AppData\Local\Temp\_MEI138122\setuptools\_vendor']
error.txt
the error of importing PyQt5 Modules:
e.msg:
DLL load failed while importing QtCore: Das angegebene Modul wurde nicht gefunden.
e.path:
C:UsersUSER~1AppDataLocalTemp_MEI254722PyQt5QtCore.pyd
script1.spec
the .spec for pyinstaller script1.spec
:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
# Import collect_all to gather all dependencies for PyQt5 modules
from PyInstaller.utils.hooks import collect_all
# Collect data, binaries, and hidden imports for PyQt5 modules
datas, binaries, hiddenimports = collect_all('PyQt5.QtCore')
datas += collect_all('PyQt5.QtWebEngineWidgets')[0]
binaries += collect_all('PyQt5.QtWebEngineWidgets')[1]
hiddenimports += collect_all('PyQt5.QtWebEngineWidgets')[2]
datas += collect_all('PyQt5.QtWidgets')[0]
binaries += collect_all('PyQt5.QtWidgets')[1]
hiddenimports += collect_all('PyQt5.QtWidgets')[2]
a = Analysis(
['script1.py'],
pathex=[],
binaries= binaries + [('.\assets\executables\*', 'assets/executables')],
datas= datas + [('.\assets\modules\*', 'assets/modules'),
('.\assets\pictures\*', 'assets/pictures'),
('.\assets\sites\*', 'assets/sites'),
('.\assets\sounds\*', 'assets/sounds') ],
hiddenimports=hiddenimports,
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
noarchive=False,
optimize=0,
)
pyz = PYZ(a.pure)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.datas,
[],
name='script1_executable',
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,
)
what my filesystem looks like
path is inside the sys_path.txt, so it should work??:
Update:
Now my application works… But I had to copy the folder C:Python312Lib
to my assets and also the C:ProjectPathvenvLibsite-packages
folder to the assets. Now my executable has an amazing size of 250MB -.-
I guess the problem were some dependend modules, either by PyQt5 or pythonw.exe itself.
Does anyone know what exactly I have to import (maybe with a --hidden-import
) to get my application smaller?
Thank you very much,
Micha