0

I have a python script which I am trying to run at boot using

sudo crontab -e

using the following command

@reboot python /usr/local/sbin/ShutdownJumper.py

the script ShutdownJumper.py begins with:

#!/usr/bin/env python
#note crontab for superuser required a new PATH variable as here https://unix.stackexchange.com/questions/43392#answer-43394
import subprocess
try:
 import RPi.GPIO as GPIO
except RuntimeError:
 print("Error importing RPi.GPIO! This is probably because you need superuser privileges. You can achieve this by using 'sudo' to run your script")

when I reboot my RPi I get the following email back:

Traceback (most recent call last):
  File "/usr/local/sbin/ShutdownJumper.py", line 4, in <module>
    import RPi.GPIO as GPIO
ImportError: No module named RPi.GPIO

I've tried changing the PATH in the crontab, so no avail..

2
  • what interpreter does python point to in your system? is RPi installed for that interpreter? is the path to RPi in your default PYTHONPATH? if not you could set sys.path to include the path to RPi in your python script. Commented Jan 28, 2017 at 9:05
  • Ok so /usr/bin/python links to /usr/bin/python2.7. If I run the script using sudo as user ubuntu, the script will run correctly. I haven't used paths in python before? Commented Jan 29, 2017 at 4:58

1 Answer 1

1

Try to use this code on crontab

@reboot cd /usr/local/sbin && sudo python ShutdownJumper.py
Sign up to request clarification or add additional context in comments.

1 Comment

works fine from the command line. Same error in my email: Traceback (most recent call last): File "ShutdownJumper.py", line 5, in <module> import RPi.GPIO as GPIO ImportError: No module named RPi.GPIO :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.