barre1986
Posts: 11
Joined: Tue Nov 01, 2016 5:29 pm

new to python code error

Sun Nov 06, 2016 10:53 am

Hi all , im new to python and allready an error
setup is RPI3 with on pin 4 i have a relay connected (sainsmart board)

Code: Select all

#!/bin/python

import RPI.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(4, GPIO.out)

try:
        while True:
        GPIO.output(4, GPIO.1)
        sleep(.2)
        GPIO.output(4, GPIO.0)

except KeyboardInterrupt:
GPIO.output(4, GPIO.1)
after executing i get the error "File 'relay1.py' line 13 GPIO.output(4, 1)
IndentationError: expected an indented block

What is wrong with my code?
Some code start with #!/bin/python
others with #!/usr/bin/python
what is the difference or best?

Major Tom
Posts: 65
Joined: Wed Feb 03, 2016 8:13 am
Location: Strung up in heavens high.

Re: new to python code error

Sun Nov 06, 2016 11:01 am

You have to indent the except clause because it's a code block:

Code: Select all

except KeyboardInterrupt:
        GPIO.output(4, GPIO.1)
Some people dislike this rule, but it's no different to C, for example, that must have matching { and }.

For your other question, here's a good discussion about it:
http://stackoverflow.com/questions/2429 ... hon-script

Edit: fixed code tags
Last edited by Major Tom on Sun Nov 06, 2016 11:28 am, edited 1 time in total.
My circuit's dead, there's something wrong.

User avatar
PeterO
Posts: 5958
Joined: Sun Jul 22, 2012 4:14 pm

Re: new to python code error

Sun Nov 06, 2016 11:10 am

Fixed code tags for you :-)
Major Tom wrote:You have to indent the except clause because it's a code block:

Code: Select all

except KeyboardInterrupt:
        GPIO.output(4, GPIO.1)
Some people dislike this rule, but it's no different to C, for example, that must have matching { and }.

For your other question, here's a good discussion about it:
http://stackoverflow.com/questions/2429 ... hon-script
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
bensimmo
Posts: 4747
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: new to python code error

Sun Nov 06, 2016 11:15 am

corrected 'code tag from above' if he doesn't notice

Code: Select all

except KeyboardInterrupt:
        GPIO.output(4, GPIO.1)
Basically
... "File 'relay1.py' line 13 GPIO.output(4, 1)
IndentationError: expected an indented block
Your file on line 13 (so it's easy to find) with the statement GPIO.output(4, 1) so you can read what may be causing it.
It is expecting an indent (tab) like in your try: section IndentationError: expected an indented block, that tab whitespace thing python likes to show what belong to which parts. Block because there may be a block of code even if just one line.

Once you figure out what the errors messages is trying to say, other errors become easier to understand and fix.

DirkS
Posts: 10398
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: new to python code error

Sun Nov 06, 2016 11:25 am

Code: Select all

        while True:
        GPIO.output(4, GPIO.1)
        sleep(.2)
        GPIO.output(4, GPIO.0)
BTW: this bit of code also suffers from an indentation problem: the code inside the while block is not properly indented.

User avatar
DougieLawson
Posts: 39624
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: new to python code error

Sun Nov 06, 2016 11:29 am

Any clause following a condition, a loop, a try: or an except: must be indented by at least one space (that's why we all love Python).

How anyone can design a language that relies on left hand whitespace still remains a mystery. How a language with such a restriction became popular also remains a complete mystery. Even COBOL added explicit scope terminators.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Major Tom
Posts: 65
Joined: Wed Feb 03, 2016 8:13 am
Location: Strung up in heavens high.

Re: new to python code error

Sun Nov 06, 2016 11:31 am

DougieLawson wrote:Any clause following a condition, a loop, a try: or an except: must be indented by at least one space (that's why we all love Python).

How anyone can design a language that relies on left hand whitespace still remains a mystery. How a language with such a restriction became popular also remains a complete mystery. Even COBOL added explicit scope terminators.
Well, I like it, and it is extremely popular and useful.
My circuit's dead, there's something wrong.

barre1986
Posts: 11
Joined: Tue Nov 01, 2016 5:29 pm

Re: new to python code error

Sun Nov 06, 2016 11:32 am

Bensimmo thx for clearing that out , didnt know the tabbing was a problem, pretty clear now

whats with the library import
sometimes its #!/bin/python or #!/usr/bin/python
is this where library is installed ( prolly) but why the difference , is there a reason for it?

Code: Select all

#!/bin/python
import RPI.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(4, GPIO.out)
try:
        while True:
                GPIO.output(4, 1)
                sleep(.2)
                GPIO.output(4, 0)

except KeyboardInterrupt:
        GPIO.cleanup()

changed code, but now got error ImportError: No module named RPI.GPIO
isnt the module included by default on RPI3?

User avatar
DougieLawson
Posts: 39624
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: new to python code error

Sun Nov 06, 2016 11:35 am

Major Tom wrote:
DougieLawson wrote:Any clause following a condition, a loop, a try: or an except: must be indented by at least one space (that's why we all love Python).

How anyone can design a language that relies on left hand whitespace still remains a mystery. How a language with such a restriction became popular also remains a complete mystery. Even COBOL added explicit scope terminators.
Well, I like it, and it is extremely popular and useful.
Except that 99.9999% of all of the python posts on here involve that strange syntactical anomaly. I don't remember a single python bug that wasn't cause by indentation or mixing spaces with tabs or something similar. When that becomes the reason why the beginner's programs don't work it makes one wonder why they ever bother moving on to writing their second python program.

That was the thing with BASIC on Sinclair's ZX80, ZX81 & Speccy you couldn't fail to get the syntax right. Python has the constant niggle of left hand whitespace to trip you up.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

User avatar
bensimmo
Posts: 4747
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: new to python code error

Sun Nov 06, 2016 11:38 am

barre1986 wrote:Bensimmo thx for clearing that out , didnt know the tabbing was a problem, pretty clear now

whats with the library import
sometimes its #!/bin/python or #!/usr/bin/python
is this where library is installed ( prolly) but why the difference , is there a reason for it?
No idea, never have it in my files and they all seem to work.
I think it's for when people launch the file directly as a executable file.
But a link was given above and no doubt it explains it in many ways one there.

if you use
python3 relay1.py
to run the programme, or from within IDLE3, I don't think it matters.
Over to the more informed for that one.

User avatar
DougieLawson
Posts: 39624
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: new to python code error

Sun Nov 06, 2016 11:39 am

barre1986 wrote: whats with the library import
sometimes its #!/bin/python or #!/usr/bin/python
is this where library is installed ( prolly) but why the difference , is there a reason for it?
It's a "shebang" line.

That's only important if you make your program have the u+x, g+w or o+x flags (with the chmod command). That tells the bash shell what program is needed to run the script.

It's a pointer to where the python program lives in the file system

Code: Select all

#!/usr/bin/python

Code: Select all

#!/usr/bin/python2

Code: Select all

#!/usr/bin/python3

Code: Select all

#!/usr/bin/env python

Code: Select all

#!/usr/bin/env python2

Code: Select all

#!/usr/bin/env python3
are the only values that work on a Raspbian system.

When you use that you can run the program with ./mypythonprogram.py rather than python mypythonprogram.py
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

barre1986
Posts: 11
Joined: Tue Nov 01, 2016 5:29 pm

Re: new to python code error

Sun Nov 06, 2016 11:42 am

Code: Select all

pi@Rasp01:~ $ python3 relay1.py
Traceback (most recent call last):
  File "relay1.py", line 2, in <module>
    import RPI.GPIO as GPIO
ImportError: No module named 'RPI'

User avatar
PeterO
Posts: 5958
Joined: Sun Jul 22, 2012 4:14 pm

Re: new to python code error

Sun Nov 06, 2016 11:44 am

barre1986 wrote:Bensimmo thx for clearing that out , didnt know the tabbing was a problem, pretty clear now

whats with the library import
sometimes its #!/bin/python or #!/usr/bin/python
is this where library is installed ( prolly) but why the difference , is there a reason for it?
It's not a library import (there is no "import" keyword)!

It's called a "she bang" https://en.wikipedia.org/wiki/Shebang_(Unix) has a full description, but in brief "#!/path/to/interpreter" at the start of an executable script tells the shell that the file contains code that it (the shell) won't understand, and instead to run the named interpreter and to pass the path of the script file to the interpreter.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
DougieLawson
Posts: 39624
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: new to python code error

Sun Nov 06, 2016 11:44 am

Python is case sensitive. So RPi.GPIO is not the same as RPI.GPIO.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

User avatar
bensimmo
Posts: 4747
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: new to python code error

Sun Nov 06, 2016 11:44 am

DougieLawson wrote:
Major Tom wrote:
DougieLawson wrote:Any clause following a condition, a loop, a try: or an except: must be indented by at least one space (that's why we all love Python).

How anyone can design a language that relies on left hand whitespace still remains a mystery. How a language with such a restriction became popular also remains a complete mystery. Even COBOL added explicit scope terminators.
Well, I like it, and it is extremely popular and useful.
Except that 99.9999% of all of the python posts on here involve that strange syntactical anomaly. I don't remember a single python bug that wasn't cause by indentation or mixing spaces with tabs or something similar. When that becomes the reason why the beginner's programs don't work it makes one wonder why they ever bother moving on to writing their second python program.

That was the thing with BASIC on Sinclair's ZX80, ZX81 & Speccy you couldn't fail to get the syntax right. Python has the constant niggle of left hand whitespace to trip you up.

Got to agree here, It's a pain in the arse sorting out mangled indents and it seems to do it itself randomly with copy and pastes between programs or different editors. :roll:
(as a beginner, BBC Basic was much easier as are Visual Basics to get along with)

Major Tom
Posts: 65
Joined: Wed Feb 03, 2016 8:13 am
Location: Strung up in heavens high.

Re: new to python code error

Sun Nov 06, 2016 11:46 am

barre1986 wrote:Bensimmo thx for clearing that out , didnt know the tabbing was a problem, pretty clear now

whats with the library import
sometimes its #!/bin/python or #!/usr/bin/python
is this where library is installed ( prolly) but why the difference , is there a reason for it?

Code: Select all

#!/bin/python
import RPI.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(4, GPIO.out)
try:
        while True:
                GPIO.output(4, 1)
                sleep(.2)
                GPIO.output(4, 0)

except KeyboardInterrupt:
        GPIO.cleanup()

changed code, but now got error ImportError: No module named RPI.GPIO
isnt the module included by default on RPI3?
Yes, but there's a small 'i' in Pi.

Code: Select all

import RPi.GPIO as GPIO
You can find the answer to your other question at the stackexchange link I posted earlier.
My circuit's dead, there's something wrong.

barre1986
Posts: 11
Joined: Tue Nov 01, 2016 5:29 pm

Re: new to python code error

Sun Nov 06, 2016 11:53 am

ok got it working
learned a lot last 30 min
thx all for the great help

Return to “Python”