tags:

views:

2118

answers:

29

I want to start using Python for small projects but the fact that a misplaced tab or indent can throw a compile error is really getting on my nerves. Is there some type of setting to turn this off?

I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?

A: 

No. How would Python parse your script if there was a way to disable this?

innaM
+26  A: 

No. Indentation-as-grammar is an integral part of the Python language, for better and worse.

Asaf Bartov
For better I feel
Teifion
Yup, for the better. It makes it harder to write hard to read code.
Echo
The question asked about available IDEs thus, this is a bad answer.
Charles Duffy
Well, it answers the stated question, rather than the implied one (is there an editor that actually handle auto-indentation, 'cause mine apparently sucks and I'm too lazy to do it myself?).
Xiong Chiamiov
@Xiong Chiamiov It isn't necessarily that the OP suck at dealing with the indention him/herself. It's perferctly possible for people to work on the same python file, and one uses this indentation, and the other uses a different one.Most IDEs won't render whitespaces by default, and it _will_ drive you nuts if you're getting a whitespace error. The lack of tokens to indicate the scope makes it impossible to get an auto-indent that would at least _show_ what the problem is.
Calyth
+2  A: 

I do not believe so, as Python is a whitespace-delimited language. Perhaps a text editor or IDE with auto-indentation would be of help. What are you currently using?

Thomas Owens
+3  A: 

No, there isn't. Indentation is syntax for Python. You can:

  1. Use tabnanny.py to check your code
  2. Use a syntax-aware editor that highlights such mistakes (vi does that, emacs I bet it does, and then, most IDEs do too)
  3. (far-fetched) write a preprocessor of your own to convert braces (or whatever block delimiters you love) into indentation
ΤΖΩΤΖΙΟΥ
For a tabnanny example see http://docs.python.org/lib/module-tabnanny.html
maccullt
+4  A: 

All of the whitespace issues I had when I was starting Python were the result mixing tabs and spaces. Once I configured everything to just use one or the other, I stopped having problems.

In my case I configured UltraEdit & vim to use spaces in place of tabs.

Mark Biek
+2  A: 

You should disable tab characters in your editor when you're working with Python (always, actually, IMHO, but especially when you're working with Python). Look for an option like "Use spaces for tabs": any decent editor should have one.

Chris Conway
Tabs are completely acceptable indentation in python.
Xiong Chiamiov
Yes, that is true. But Python has one interpretation of tabs and your text editor may have another. For this reason, the Python language manual recommends not mixing tabs and spaces in indentation. IMHO, the easiest way to guarantee this is to always use spaces.
Chris Conway
A: 

Not really. There are a few ways to modify whitespace rules for a given line of code, but you will still need indent levels to determine scope.

You can terminate statements with ; and then begin a new statement on the same line. (Which people often do when golfing.)

If you want to break up a single line into multiple lines you can finish a line with the \ character which means the current line effectively continues from the first non-whitespace character of the next line. This visually appears violate the usual whitespace rules but is legal.

My advice: don't use tabs if you are having tab/space confusion. Use spaces, and choose either 2 or 3 spaces as your indent level.

A good editor will make it so you don't have to worry about this. (python-mode for emacs, for example, you can just use the tab key and it will keep you honest).

Justin Standard
+1  A: 

It's possible to write a pre-processor which takes randomly-indented code with pseudo-python keywords like "endif" and "endwhile" and properly indents things. I had to do this when using python as an "ASP-like" language, because the whole notion of "indentation" gets a bit fuzzy in such an environment.

Of course, even with such a thing you really ought to indent sanely, at which point the conveter becomes superfluous.

+2  A: 

I agree with justin and others -- pick a good editor and use spaces rather than tabs for indentation and the whitespace thing becomes a non-issue. I only recently started using Python, and while I thought the whitespace issue would be a real annoyance it turns out to not be the case. For the record I'm using emacs though I'm sure there are other editors out there that do an equally fine job.

If you're really dead-set against it, you can always pass your scripts through a pre-processor but that's a bad idea on many levels. If you're going to learn a language, embrace the features of that language rather than try to work around them. Otherwise, what's the point of learning a new language?

Bryan Oakley
+1  A: 

Getting your indentation to work correctly is going to be important in any language you use.

Even though it won't affect the execution of the program in most other languages, incorrect indentation can be very confusing for anyone trying to read your program, so you need to invest the time in figuring out how to configure your editor to align things correctly.

Python is pretty liberal in how it lets you indent. You can pick between tabs and spaces (but you really should use spaces) and can pick how many spaces. The only thing it requires is that you are consistent which ultimately is important no matter what language you use.

indentation
+4  A: 

I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?

I liked pydev extensions of eclipse for that.

Raz
+2  A: 

Tabs and spaces confusion can be fixed by setting your editor to use spaces instead of tabs.

To make whitespace completely intuitive, you can use a stronger code editor or an IDE (though you don't need a full-blown IDE if all you need is proper automatic code indenting).

A list of editors can be found in the Python wiki, though that one is a bit too exhausting: - http://wiki.python.org/moin/PythonEditors

There's already a question in here which tries to slim that down a bit:

Maybe you should add a more specific question on that: "Which Python editor or IDE do you prefer on Windows - and why?"

A: 

I was a bit reluctant to learn Python because of tabbing. However, I almost didn't notice it when I used Vim.

metadave
+2  A: 

I find it hard to understand when people flag this as a problem with Python. I took to it immediately and actually find it's one of my favourite 'features' of the language :)

In other languages I have two jobs: 1. Fix the braces so the computer can parse my code 2. Fix the indentation so I can parse my code.

So in Python I have half as much to worry about ;-)

(nb the only time I ever have problem with indendation is when Python code is in a blog and a forum that messes with the white-space but this is happening less and less as the apps get smarter)

Andy Baker
A: 

If you're looking for a recommendation for a Python IDE, after extensive research, I've been most happy with Wing Software's WingIDE:

http://www.wingware.com/products

There is a free trial version, so you have nothing to lose. It supports all the major OSes, and is only about $60 for the full version.

I also like SciTE a lot, which is totally free, free, free!

http://scintilla.sourceforge.net/SciTEDownload.html

I use emacs for writing and WingIDE for debugging. It's a good combo.
A: 

Many Python IDEs and generally-capable text/source editors can handle the whitespace for you.

However, it is best to just "let go" and enjoy the whitespace rules of Python. With some practice, they won't get into your way at all, and you will find they have many merits, the most important of which are:

  1. Because of the forced whitespace, Python code is simpler to understand. You will find that as you read code written by others, it is easier to grok than code in, say, Perl or PHP.
  2. Whitespace saves you quite a few keystrokes of control characters like { and }, which litter code written in C-like languages. Less {s and }s means, among other things, less RSI and wrist pain. This is not a matter to take lightly.
Eli Bendersky
+2  A: 

pybraces

It's unsupported.

nt
A: 

In Python, indentation is a semantic element as well as providing visual grouping for readability.

Both space and tab can indicate indentation. This is unfortunate, because:

  • The interpretation(s) of a tab varies among editors and IDEs and is often configurable (and often configured).

  • OTOH, some editors are not configurable but apply their own rules for indentation.

  • Different sequences of spaces and tabs may be visually indistinguishable.

  • Cut and pastes can alter whitespace.

So, unless you know that a given piece of code will only be modified by yourself with a single tool and an unvarying config, you must avoid tabs for indentation (configure your IDE) and make sure that you are warned if they are introduced (search for tabs in leading whitespace).

And you can still expect to be bitten now and then, as long as arbitrary semantics are applied to control characters.

A: 

The real answer to your question is that if you are going to use the language you need to learn its syntax. Just as an error in indenting python can generate a compiler error, an error using braces in various other languages can also generate a compiler error.

Even worse it can be silently misinterpreted by the compiler to do the wrong thing. This is particularly dangerous when the indenting doesn't match the desired meaning. I.e. in many other languages:

If(first condition)
   if (second condition)
      do something interesting;
else
  do something different;

Will lead to unpleasant surprises.

Python forces you to write code that looks like what it does. This is a good thing for other programmers who have to read your code, or for you when you try to read your own code after a month or so.

Dale Wilson
A: 

If you don't want to use an IDE/text editor with automatic indenting, you can use the pindent.py script that comes in the Tools\Scripts directory. It's a preprocessor that can convert code like:

def foobar(a, b):
if a == b:
a = a+1
elif a < b:
b = b-1
if b > a: a = a-1
end if
else:
print 'oops!'
end if
end def foobar

into:

def foobar(a, b):
   if a == b:
       a = a+1
   elif a < b:
       b = b-1
       if b > a: a = a-1
       # end if
   else:
       print 'oops!'
   # end if
# end def foobar

Which is valid python.

Ryan
But why would want to write code like in the first example? It's harder to read.
Echo
You would only input it the first way. Perhaps you have physical limitations, such as being visually impaired and using a screen reader, or are copy/pasting code from something that eats tabs.
Ryan
How does it know that the second "if" goes inside the "elif", or that the final "else" doesn't?
detly
The same way that other compilers match up parenthesis. It keeps it on a stack :)
Ryan
Ah, I did not notice the `end if` lines! Makes more sense.
detly
+8  A: 

Emacs! Seriously, its use of "tab is a command, not a character", is absolutely perfect for python development.

duane
+20  A: 
from __future__ import braces
Chris Calloway
A: 

Check the options of your editor or find an editor/IDE that allows you to convert TABs to spaces. I usually set the options of my editor to substitute the TAB character with 4 spaces, and I never run into any problems.

Curro
A: 

Strange - No one mentioned GEdit (Gnome) or OpenKomodo (Windows, Mac, GNU/Linux...). Both of them are great!

OpenKomodo especially deals with tabs and spaces very well. And - it's free. Whee! When I need a lighter weight thingy, I just use GEdit.

Download OpenKomodo here - http://www.openkomodo.com/

Kelvin Quee
A: 

Nope, there's no way around it, and it's by design:

>>> from __future__ import braces
  File "<stdin>", line 1
SyntaxError: not a chance

Most Python programmers simply don't use tabs, but use spaces to indent instead, that way there's no editor-to-editor inconsistency.

Dan
A: 

Yes, there is a way. I hate these "no way" answers, there is no way until you discover one.

And in that case, whatever it is worth, there is one.

I read once about a guy who designed a way to code so that a simple script could re-indent the code properly. I didn't managed to find any links today, though, but I swear I read it.

The main tricks are to always use return at the end of a function, always use pass at the end of an if or at the end of a class definition, and always use continue at the end of a while. Of course, any other no-effect instruction would fit the purpose.

Then, a simple awk script can take your code and detect the end of block by reading pass/continue/return instructions, and the start of code with if/def/while/... instructions.

Of course, because you'll develop your indenting script, you'll see that you don't have to use continue after a return inside the if, because the return will trigger the indent-back mechanism. The same applies for other situations. Just get use to it.

If you are diligent, you'll be able to cut/paste and add/remove if and correct the indentations automagically. And incidentally, pasting code from the web will require you to understand a bit of it so that you can adapt it to that "non-classical" setting.

Gzorg
A: 

I'm surprised no one has mentioned IDLE as a good default python editor. Nice syntax colors, handles indents, has intellisense, easy to adjust fonts, and it comes with the default download of python. Heck, I write mostly IronPython, but it's so nice & easy to edit in IDLE and run ipy from a command prompt.

Oh, and what is the big deal about whitespace? Most easy to read C or C# is well indented, too, python just enforces a really simple formatting rule.

Phil
A: 

Just use Ruby, it's much better than Python.

http://www.ruby-lang.org

Diego
Perl is arguably a closer match, if you're looking for Python with braces...
rplevy
A: 

Any decent programming editor will reduce this annoyance to nil. I see Notepad++ has syntax highlighting, it's likely you just need to set your preferences appropriately for auto-indenting and such.

jkyle