Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,696 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 67,990 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,624 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 67,990 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,624 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 67,990 Points

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

Jason Anders
Jason Anders
Treehouse Moderator 145,624 Points

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