I'm currently using Komodo Edit in Windows 7, however, I have experienced this problem on my Mac with TextWrangler. For whatever reason I'm getting some kind of whitespace error which is a huge problem when I'm writing in Python. For example, everything appears to be properly tabbed, but Komodo is currently giving me an "Ambiguous whitespace" error
//How it appears in my editor
def SaveList(self, directory):
templist = []
templistbox2 = []
for n,i in enumerate(self.listbox2.get(0,END)):
templistbox2.insert(n, re.sub(r'^[0-9]*[.]',"",str(i)))
for filename in sorted(os.listdir(directory)):
self.templist.insert(i, filename)
print filename #whitespace error here
Considering I've experienced this with two different editors on both windows and Mac, I'm wondering if there's some setting I don't know about, or if I'm doing something wrong.
When I copy your code to a file, test.py, and run
cat -A test.py
I see
//How it appears in my editor$
def SaveList(self, directory):$
templist = []$
templistbox2 = []$
for n,i in enumerate(self.listbox2.get(0,END)): $
templistbox2.insert(n, re.sub(r'^[0-9]*[.]',"",str(i)))$
for filename in sorted(os.listdir(directory)):$
self.templist.insert(i, filename)$
^I print filename #whitespace error here$
which indicates there is a tab (represented by ^I
) followed by four spaces on the last line.
I'm not sure what the equivalent tool on Windows would be, but the Mac should have the cat -A
command. It will show you where the tabs versus spaces are.
There is a program called reindent.py which will convert tabs to spaces for you:
reindent.py test.py
On Unix there is also a unexpand command which converts spaces to tabs.
Most Python programmers use spaces rather than tabs for indentation. Most of the Python code you find on the web will use spaces rather than tabs.
Your editor may be adding tabs, but if you took a snippet of code from the web, your file may now contain both tabs and spaces.
It is easiest to go with the flow and adopt the spaces-as-indentation convention, so you will not have to reformat other people's code so much.
By the way, adopting the spaces-as-indentation convention does not mean having to press SPACE 4 times for each indentation level. Your editor should have a configuration option which makes pressing TAB insert 4 spaces.
This is a common problem in Python. The following advice may help you:
1) Never mix spaces and tabs. For new projects, use spaces rather than tabs See PEP8 My recommendation is to use 4 spaces.
2) Change the default values for tabs length in Komodo, in order to detect the mixes more easily . Press the Edit > Preferences
menu, then in the Editor settings
:
Prefer Tab characters over spaces
.Number of spaces for indent
width of each tab character
3) The reindent.py
script in C:\Python2x\Tools\Scripts\
can help you to reindent files properly
-d (--dryrun) Dry run. Analyze, but don't make any changes to, files.
-r (--recurse) Recurse. Search for all .py files in subdirectories too.
-n (--nobackup) No backup. Does not make a ".bak" file before reindenting.
-v (--verbose) Verbose. Print informative msgs; else no output.
-h (--help) Help. Print this usage information and exit.
Change Python (.py) files to use 4-space indents and no hard tab characters.
Also trim excess spaces and tabs from ends of lines, and remove empty lines
at the end of files. Also ensure the last line ends with a newline.
I hope it helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With