Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,734 Points

I still don't understand whitespace

This is my code:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def homepage():
    try:
        return "This is some test text"
    except Exception, e: 
        return str(e)

if __name__ == "__main__":
    app.run()

This is my error message:

  File "__init__.py", line 9
    except Exception, e:
                       ^
IndentationError: unindent does not match any outer indentation level

I'm using Sublime Text 2 and it automatically creates tabs when I've started a code block. Most of the time this doesn't seem to cause any problems. In this case, I've tried changing the tabs to spaces, same error. Also, I tried reading this person's blog to figure it out: http://www.secnetix.de/olli/Python/block_indentation.hawk

I don't get it. What are the best practices with whitespace in python? Should I change the default behavior of my text editor?

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The best practice according to PEP-8 is to use 4-space indentation.

As in the docs your except syntax should be:

    except Exception as e:
Jason Anders
Jason Anders
Treehouse Moderator 145,857 Points

Thanks Chris Freeman

I haven't looked at Python for a bit. Thank you for correcting the amount of spaces to 4.

Is it Ruby that is 2 spaces, then? I really thought Python was two... :confused:

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

I hadn't read the blog before. It seems more of a How-To on pushing the boundaries. The issue with non-PEP-8 coding is it leaves code that others have to live with or fix for compliance.

When in doubt, run pylint or flake8 to check for non-standard formatting.

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,857 Points

I'm not 100%, but if I remember correctly, Python uses 2 spaces as indentation. So, if you set Sublime to 2 spaces as indentation (pressing tab), you should be okay. Again, I don't do much Python, but give that a try.

So this:

@app.route('/')
def homepage():
    try:
        return "This is some test text"
    except Exception, e: 
        return str(e)

if __name__ == "__main__":
    app.run()

Would look like this:

@app.route('/')
def homepage():
  try:
    return "This is some test text"
  except Exception, e: 
    return str(e)

if __name__ == "__main__":
  app.run()

:)

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Ack! Do not use 2-space indentation. See PEP-8 comment above. Use 4-space indentation.

Jason Anders
Jason Anders
Treehouse Moderator 145,857 Points

I knew I wasn't sure.... Is it Ruby that is 2 space??