I am having a really strange issue. My script works when I run it from within Visual Studio Code, but when I go to the terminal and type the command line below, it does not work. The code detects when a USB device is inserted. When running from the terminal, the "Device Detected" line never appears.
python3.6 MAINtest.py
When running from the terminal, something happens where either the QProcess does not run, or it never detects output from usbDetectTEST.py . Again, this code works when I run it from within Visual Studio Code. What is the difference between running it there vs from the commandline?
These are the three files that replicate the issue I am having. Any insight would be greatly appreciated.
MAINtest.py
import sys
import time
import subprocess
from CLASSEStest import detectUSB # a file with the detectUSB class
from PyQt5 import QtCore, QtGui, QtWidgets, uic
qtcreator_file = "GUI.ui" # This can be any .ui file, it does not matter
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtcreator_file)
class MyWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
self.detectLogs = detectUSB()
self.detectLogs.deviceDetected.connect(self.RefreshLogs2)
self.detectLogs.begin()
def RefreshLogs2(self):
print("Device Detected")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
CLASSEStest.py
import sys
from PyQt5 import QtCore, QtGui
import pyudev
import time
class detectUSB(QtCore.QObject):
deviceDetected = QtCore.pyqtSignal()
def __init__(self, parent=None):
QtCore.QObject.__init__(self, parent)
def begin(self):
process = QtCore.QProcess(self)
process.readyReadStandardOutput.connect(self.ReadyReadReady)
process.start("python3.6 usbDetectTEST.py")
process.waitForStarted()
def ReadyReadReady(self):
time.sleep(2) # it outputs 2 things every time one USB is detected unless this is here
self.deviceDetected.emit()
usbDetectTEST.py
# Credit: https://avilpage.com/2016/09/detecting-device-events-in-ubuntu-with-python.html
import pyudev
import sys
import time
context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by(subsystem='usb')
for device in iter(monitor.poll, None): # iter(monitor.poll, None)
if device.action == 'add':
#print(device)
print(device.action, file = sys.stdout)
Update: I added " print(QtCore.QFile.exists("/my/path/usbDetectTEST.py")) " in the class right before it starts the program, and it sent back "False". So it cannot find the usbDetectTEST.py file. Not sure why...