0

I'm trying to run this Python script:

import os

with open('check_door', 'r') as file:
    text_1 = file.readline()

with open('alert_check', 'r') as file2:
    text_2 = file2.readline()

if text_1 == 'locked' and text_2 == 'disabled':
    os.system('python sensor.py')

using this command: watch -n 60 /home/pi/check_sensor.py I'm getting these errors:

/home/pi/check_sensor.py: 1: /home/pi/check_sensor.py: import: not found
/home/pi/check_sensor.py: 2: /home/pi/check_sensor.py: Syntax error: "(" unexpected

What am I doing wrong ?

  • You need the Python shebang at the beginning of your script if you want to run it as an executable, unless you run the script with the `python` command. See: https://stackoverflow.com/questions/6908143/should-i-put-shebang-in-python-scripts-and-what-form-should-it-take – sudo Jul 27 '18 at 21:20

1 Answers1

0

You need to invoke the python interpreter, as you would do if you were running the script directly.

watch -n 60 "python /home/pi/check_sensor.py"
i alarmed alien
  • 9,412
  • 3
  • 27
  • 40
  • 1
    `#!/usr/bin/env python` would be more conventional -- that way the script itself chooses its interpreter, so you can change the interpreter without modifying anything but the script text itself. – Charles Duffy Jul 27 '18 at 21:26
  • Also, note the "Answer Well-Asked Questions" section in [How to Answer](https://stackoverflow.com/help/how-to-answer), particularly the bullet point regarding questions which "*have already been asked and answered many times before*". – Charles Duffy Jul 27 '18 at 21:30
  • Looks like the "more conventional" way also works. Thank you both for helping me ! – Tom Shanks Jul 27 '18 at 21:33