0

I have a python script which detects the motion from webcam or a video. I need a GUI to start this python script. I created my GUI by using PyQt5 Designer and I want to click a button to open my motion_detector.py

Here is my code.

I have a button in GUI

  self.pushButton.setObjectName("pushButton")

I try to open my motion_detector.py by clicking this button

  self.pushButton.clicked.connect(lambda:os.system('python motion_detector.py'))

At the top of my code I imported "os" and also I put my GUI file untitled.py and my motion_detector.py in the same directory. When I try to run it gives me an error

  python: can't open file 'motion_detector.py': [Errno 2] No such file or directory

Thank you for your help!

furas
  • 134,197
  • 12
  • 106
  • 148
Yasin Demirkaya
  • 243
  • 6
  • 22

1 Answers1

0

You can use either subprocess or os modules. I prefer subprocess since it's quieter and it stores for you the output and the error code (if there's one) - in both cases don't forget to put the full path to both your script and the python.exe (unless you put it's path to the %PATH% variable)

The syntax for os.system is:

os.system("<path to python> <path to your script>")

The syntax for subprocess.Popen is:

proc = subprocess.Popen(t"<path to python>", "<path to your script>"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
stdout, stderr = proc.communicate()
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • You want to avoid a shell with `Popen`. See https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess – tripleee Dec 25 '19 at 21:01
  • My bad forgot to add the shell=False. Thanks for the comment! –  Dec 25 '19 at 21:03
  • That's not sufficient; you also have to convert the first argument to a list of strings. (Edited to fix.) But you really want to avoid `Popen` altogether for `run` or a similar higher-level function. See further https://stackoverflow.com/a/51950538/874188 – tripleee Dec 25 '19 at 21:38