I’m getting through the book “Create GUI Applications with Python & Qt6 Pyside6 Edition” and I like it a lot
The problem I’m stuck with is related to QProcesses. When I run qrunnable_process_parser.py with dummy_scripy.py it works fine. However, when I add my own script with “import requests” I get the error: ModuleNotFoundError: No module named ‘requests’, even though it is installed.
Has anyone of you guys encountered such a problem?
Welcome to the forum! If you’re getting an import error in the dummy script but you’re sure it’s installed in your apps environment, it likely means that the script is being run with a different version of Python to your app.
Do you run your app using python3? In that case you’ll want to run the script with python3 too.
If that isn’t it let me know & I can give you some code to debug to debug the problem.
python --version
Python 3.12.3
python3 --version
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
So using just “python” is the valid option (it’s the only one I have installed).
When I run qrunnable_process_parser.py with the default (i.e. taken from the book) dummy_script.py all is fine.
…
self.runner = SubProcessWorker(
command=“python dummy_script.py”,
…
However when I modify it, i.e. add additional imports that I’d like to use at the beginning of the file:
import requests
import cv2
it suddenly throws the error: ModuleNotFoundError: No module named ‘requests’ etc.
Default dummy_script imports built-in modules: sys, time.
Martin, have you tried to import any third-party modules like requests, opencv or anything else in the dummy_script?
If the Python is set up properly, the imports should work fine. It’s just running as any other Python script. First I suggest adding the following to the dummy_script.py file, so you can see what you’re working with.
For the subprocess example you’re looking at, you need to do something like
import os
# ... snip ...
with subprocess.Popen(
self.command,
bufsize=1,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
env=os.environ,
) as proc:
…that is, import os and pass os.environ to the Popen call.
If the Python executable is correct, I suspect the issue is that the Python path is not being set correctly. For the QProcess examples you can copy the system environment over to the process you’re running with the following.
from PySide6.QtCore import QProcess, QProcessEnvironment
process = QProcess()
environment = QProcessEnvironment.systemEnvironment()
process.setProcessEnvironment(environment)