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 ?

1
  • 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: stackoverflow.com/questions/6908143/… Commented Jul 27, 2018 at 21:20

1 Answer 1

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"
Sign up to request clarification or add additional context in comments.

3 Comments

#!/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.
Also, note the "Answer Well-Asked Questions" section in How to Answer, particularly the bullet point regarding questions which "have already been asked and answered many times before".
Looks like the "more conventional" way also works. Thank you both for helping me !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.