Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Qt errors when starting as a subprocess in python #119

Open
lokinou opened this issue Oct 23, 2024 · 0 comments
Open

Qt errors when starting as a subprocess in python #119

lokinou opened this issue Oct 23, 2024 · 0 comments

Comments

@lokinou
Copy link

lokinou commented Oct 23, 2024

I had an issue with Qt libraries issues when calling LabRecorder as a subprocess from python.

When using subprocess.Popen(), the current os.environ variables are passed along.

Unfortunately LabRecorder seems to first lookup Qt libraries in the PATH rather than use those carried on during compilation.
To solve the issue, I manually remove them from the environment

import os
from subprocess import Popen, PIPE

# we need to strip the PATH variable of any pyqt reference passed on when opening the pipe!
env_dict = dict(copy.deepcopy(os.environ))
pth = env_dict.get("PATH")
pth = ";".join([s for s in pth.split(";") if s.lower().find("pyqt") < 0]) # remove qt
print(f"new path without qt: {pth}")
for key, val in env_dict.items():
    print(f"{key}: {val}")

# here are the culprit that breaks qt from working on the called executable
if "QT_PLUGIN_PATH" in env_dict.keys():
    del env_dict["QT_PLUGIN_PATH"]
if "QML2_IMPORT_PATH" in env_dict.keys():
    del env_dict["QML2_IMPORT_PATH"]

Then I can call the subprocess with the altered environment variables

cmd = ['Labrecorder.exe']
working_directory = '.'
self.process = Popen(cmd,
                     stdout=PIPE, 
                     stderr=PIPE,
                     cwd=working_directory ,
                     shell=True,
                     env=env_dict,
                     close_fds=True,
                     creationflags=subprocess.CREATE_NEW_PROCESS_GROUP \
                                   | subprocess.DETACHED_PROCESS \
                                   | subprocess.CREATE_DEFAULT_ERROR_MODE
                     )

Here is the fix, in case nothing can be done :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant