Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1584

Python • Re: Button to restart Pi, button to restart program

$
0
0
For restarting the script, you could try something like this:

Code:

from gpiozero import Buttonimport subprocessfrom signal import pauseimport sysdef restart_program():   try:      subprocess.run(['python', 'test.py'])   except KeyboardInterrupt:      print("Exiting script...")      sys.exit()   finally:      sys.exit()my_button = Button(17)my_button.when_pressed = restart_programpause()
Note: this method involves creating another script that when the button is clicked runs you original script.

I am trying the above method now. But have a problem. My main.py has an imported module (RPLCD) to write to LCD. I have created a virtual environment called "venv" that I installed the module in and run it in. The program works when the environment is activated, but not when called by another program, (no module named 'RPLCD').

button_monitortwo.py starts and is going to control main.py.

How can I handle this situation when a virtual environment is required? I have tried shebang: #!/usr/bin/env python3 , but same error.

Statistics: Posted by BlueBarryCat — Sun Aug 25, 2024 3:20 pm



Viewing all articles
Browse latest Browse all 1584

Trending Articles