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

Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsI 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

Jason Anders
Treehouse Moderator 145,860 PointsI'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
Treehouse Moderator 68,460 PointsAck! Do not use 2-space indentation. See PEP-8 comment above. Use 4-space indentation.

Jason Anders
Treehouse Moderator 145,860 PointsI knew I wasn't sure.... Is it Ruby that is 2 space??
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsBrendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsSo is that person's blog wrong?
Jason Anders
Treehouse Moderator 145,860 PointsJason Anders
Treehouse Moderator 145,860 PointsThanks 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...
Chris Freeman
Treehouse Moderator 68,460 PointsChris Freeman
Treehouse Moderator 68,460 PointsI 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
orflake8
to check for non-standard formatting.