python import PyQt5 working in debug, not in executable

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật