0

I am having a problem with a (apparently) very commonly used script for relay automation.

The script is as follows:

 #!/usr/bin/python
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

# init list with pin numbers

pinList = [2, 3, 4, 17, 27, 22, 10, 9]

# loop through pins and set mode and state to 'low'

for i in pinList: 
    GPIO.setup(i, GPIO.OUT) 
    GPIO.output(i, GPIO.HIGH)

# time to sleep between operations in the main loop

SleepTimeL = 2

# main loop

try:
  GPIO.output(2, GPIO.LOW)
  print "ONE"
  time.sleep(SleepTimeL); 
  GPIO.output(3, GPIO.LOW)
  print "TWO"
  time.sleep(SleepTimeL);  
  GPIO.output(4, GPIO.LOW)
  print "THREE"
  time.sleep(SleepTimeL);
  GPIO.output(17, GPIO.LOW)
  print "FOUR"
  time.sleep(SleepTimeL);
  GPIO.output(27, GPIO.LOW)
  print "FIVE"
  time.sleep(SleepTimeL);
  GPIO.output(22, GPIO.LOW)
  print "SIX"
  time.sleep(SleepTimeL);
  GPIO.output(10, GPIO.LOW)
  print "SEVEN"
  time.sleep(SleepTimeL);
  GPIO.output(9, GPIO.LOW)
  print "EIGHT"
  time.sleep(SleepTimeL);
  GPIO.cleanup()
  print "Good bye!"

# End program cleanly with keyboard
except KeyboardInterrupt:
  print "  Quit"

  # Reset GPIO settings
  GPIO.cleanup()

I have the latest Wheezy image as well as Phyton (2.6, 2.7 & 3, GPIO-0.5.2a & 0.5.5) .. When I use

./test.sh (my script is named "test")

I get:

Traceback (most recent call last):
  File "./test.sh", line 5, in <module>
    GPIO.setmode(GPIO.BCM)
AttributeError: 'module' object has no attribute 'setmode'

So then I use:

bash -x text.sh

and get

+ import RPi.GPIO as GPIO
test.sh: line 2: import: command not found
+ import time
test.sh: line 3: import: command not found
+import time
test.sh: line 5: syntax error near unexpected token 'GPIO.BCM'
test.sh: line 5: 'GPIO.setmode(GPIO.BCM)

I have also tried

GPIO.setmode(BCM)

with the same results.

Any suggestions would be immensely appreciated. I am a complete n00b with the pi and this has been driving me nuts for....days.

4
  • 1
    You are probably using an old version of RPi.GPIO.
    – joan
    Commented Jun 9, 2014 at 16:41
  • I have the latest Wheezy image as well as Phyton (2.6, 2.7 & 3, GPIO-0.5.2a & 0.5.5) I'm actually in the process of flashing a new card with a fresh install. It just seems that something has got to be corrupt, somewhere, as it all "should" work.
    – DB28704
    Commented Jun 9, 2014 at 16:42
  • There appears to be a space before the #! in the first line. You should remove the space.
    – joan
    Commented Jun 9, 2014 at 18:57
  • Do you have any other files in this dir? Also, can you run: python -c "import RPi.GPIO as GPIO; print GPIO.__file__" and paste the result?
    – salmon
    Commented Mar 6, 2015 at 22:57

3 Answers 3

1

Why don't you just make test.sh run the following

sudo python YourPythonCode.py

remember to make it executable

chmod 755 test.sh

copy all your listed code into YourPythonCode.py

3
  • Thank you. I have changed the extension to .py as a friend had suggested earlier, too, but it still doesn't seem to be functioning. I think now I may have addressed another issue in that I believe it may have been that I wasn't executing commands as root, earlier? Only thing is, now, when I execute commands such as ./test.ph I get: -bash: ./test.sh: no such file or directory
    – DB28704
    Commented Jun 9, 2014 at 16:58
  • 1
    you need to change to the directory where the code is located. to view the files in a directory, type "ls -al" To change to a directory, use the "cd" command e.g. "cd Documents/mycodes" you know you can run the python codes directly right?? does it work like that?? type "python /your-code-path/YourPythonCode.py"
    – Akintoyeii
    Commented Jun 9, 2014 at 17:19
  • I did know you could run them directly, yes. Unfortunately none of mine were doing even that. Thank you very much, again. I am updating my new install on a second card, which is a larger card, anyway. I'll update how that goes.
    – DB28704
    Commented Jun 9, 2014 at 17:27
0
 #!/usr/bin/python
import RPi.GPIO as GPIO

The mistake is right here, in the first line. A shebang must begin at the very first character of the script, while in your code it's preceded by a space character. This makes the script be interpreted by your default shell, rather than a Python interpreter.

Remove the space to make first two characters #!, and the error should vanish.

-1

Rename your script to test.py and then run it with sudo python test.py (sudo is needed for GPIO access)

2
  • GPIO works perfectly fine without root access for me.
    – Ruslan
    Commented Apr 4, 2021 at 13:03
  • @Ruslan that's new, back in 2015 (!), sudo was required
    – developius
    Commented Apr 19, 2021 at 21:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.