I\'m trying to create an application in Python 3.2 and I use tabs all the time for indentation, but even the editor changes some of them into spaces and then print out \"inc
With the IDLE editor you can use this:
There was a duplicate of this question from here but I thought I would offer a view to do with modern editors and the vast array of features they offer. With python code, anything that needs to be intented in a .py
file, needs to either all be intented using the tab key, or by spaces. Convention is to use four spaces for an indentation. Most editors have the ability to visually show on the editor whether the code is being indented with spaces or tabs, which helps greatly for debugging. For example, with atom, going to preferences and then editor you can see the following two options:
Then if your code is using spaces, you will see small dots where your code is indented:
And if it is indented using tabs, you will see something like this:
Now if you noticed, you can see that when using tabs, there are more errors/warnings on the left, this is because of something called pep8 pep8 documentation, which is basically a uniform style guide for python, so that all developers mostly code to the same standard and appearance, which helps when trying to understand other peoples code, it is in pep8 which favors the use of spaces to indent rather than tabs. And we can see the editor showing that there is a warning relating to pep8 warning code W191
,
I hope all the above helps you understand the nature of the problem you are having and how to prevent it in the future.
Solving this using Vim editor
cd <path_to_your_directory>
). Ex: cd /home/vineeshvs/work
.vim <file_name>
). Ex: vim myfile.txt
.:set hlsearch
):<line_number>
). Ex: :53
in Vim editor after pressing ESC button once.:.,$s/\t/<give_as_many_spaces_as_you_want_to_replace_tab>/gc
). Ex: Tab will be replaced with four spaces using the following command: :.,$s/\t/ /gc
after pressing ESC button once). This process is interactive. You may give y
to replace the tab with spaces and n
to skip a particular replacement. Press ESC when you are done with the required replacements.I had the same problem and fix it using following python script. hope it help others.
it is because of using tabs and spaces for indenting code. in this script I replace each tab with four spaces.
input_file = "source code path here" # e.g. source.py
output_file = "out put file path here" # e.g out.py
with open(input_file, 'r') as source:
with open(output_file, 'a+') as result:
for line in source:
line = line.replace('\t', ' ')
result.write(line)
if you use sublime or any other editor which gives you the tool to replace text you can replace all tabs by four spaces from editor.
Ctrl+Shift+P or View->Command Palette.
Type
>Convert Indentation to Spaces
press Enter.
What I did when the same error popped up: Select everything (Str + A) and press Shift + Tab. So nothing was indented anymore. Now go back to the lines you want to have indented, and put it back how you want it.
It worked for me...