There is no universal tab size, so I always make sure to replace tabs by spaces (so you know what you see is what you get everywhere else as well)
Go to Settings -> "Preferences..." -> Language Menu/Tab Settings and check 'Replace by space'
Answer from ikottman on Stack OverflowThere is no universal tab size, so I always make sure to replace tabs by spaces (so you know what you see is what you get everywhere else as well)
Go to Settings -> "Preferences..." -> Language Menu/Tab Settings and check 'Replace by space'
I would suggest going to View > Show Symbol > Show Whitespace and Tab to get an better idea of how your indentations look.
How to fix Python indentation - Stack Overflow
Automatic indentation for Python in Notepad++ - Stack Overflow
Indentation errors in python - Stack Overflow
What is the quickest and easiest way to fix indentation errors?
Use the reindent.py script that you find in the Tools/scripts/ directory of your Python installation:
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.
Have a look at that script for detailed usage instructions.
NOTE: If your linux distro does not have reindent installed by default with Python:
Many linux distros do not have reindent installed by default with python --> one easy way to get reindent is to do pip install reindent.
p.s. An alternative to pip is to use your distros package manager (i.e. apt-get, yum, dnf) but then you need to figure out what package has the command line tool because each distro has the tool in a different package.
I would reach for autopep8 to do this:
$ # see what changes it would make
$ autopep8 path/to/file.py --select=E101,E121 --diff
$ # make these changes
$ autopep8 path/to/file.py --select=E101,E121 --in-place
Note: E101 and E121 are pep8 indentation (I think you can simply pass --select=E1 to fix all indentation related issues - those starting with E1).
You can apply this to your entire project using recursive flag:
$ autopep8 package_dir --recursive --select=E101,E121 --in-place
See also Tool to convert Python code to be PEP8 compliant.
This is what you want:
Settings > Preferences > MISC. > Auto-Indent (checkbox)
--
Sometimes people ask, "How can I do x in program y?"
I have a dream that one day "Use program z instead" will not be the most popular response.
I played a bit with python in Notepad++ and I had a problem with the tab. Since in python your indentation is really important but notepad ++ put space instead of a tab.
So to change to tab you need to go in notepad ++ Menu Settings > Preferences... then select Tab Settings Then select python in the Tab Settings box and uncheck Use default value and check Replace by space.
Like the other answer, you might want to consider another ide. I personally use eclipse with the pydev plugin and django plungin. Erik is nice too.
Sounds like you have mixed tabs and spaces in your block. Use one or the other, but not both.
There is no way to auto-indent python without understanding the code (as @alKid and @Daniel say in the comments):
For example:
if(x < y):
# Since there are no ending brackets
# how will the program
# know when you want
# to exit the indentation block?
Context - I've been writing Python for a good number of years and I still find indentation errors annoying. Also I'm using VScode with the Python extension.
How often do you encounter them? How are you dealing with them?
Because in Javascript land (and other languages too), there are some linters that look to be taking care of that.
I'm having a headache trying to remove all the indentation errors from my code. I put it in python 2.7 and no matter what, with both four-spaces and the actual tab key, it's been giving me an indentation error. With python 3.4 it's the same error, same story, but in a different place. As far as I can tell, the syntax itself is sound (and on the proper level of indentation). I have the file open in Notepad++ and everything seems to be showing up as four-spaced (instead of with tab characters).
Is there any easy way to hammer out indentation errors like this? Or am I stuck manually retabbing everything?
edit: I fixed my specific problem, but I'm still interested in learning if there's a general way to solve tabbing errors.
Here's the problem code (according to 3.4):
try:
response = requests.post("https://www.reddit.com/api/v1/access_token", auth=client_auth, data=inputData, headers=inputHeaders)
rawinfo = response.content
jsoninfo = json.loads(rawinfo)
token = jsoninfo["access_token"]
print getTimestamp(), "Token retrieved."
return token
#returns token which can be used for an hour
specifically, print getTimestamp.
If IndentationError: unexpected indent is your error in 2.7, check that all indents are tabs or 4 spaces, and not a mix of the two. Try regex searching for any \t's if you're using an editor that allows that. In python 3, the error for tab/space mixing is TabError: inconsistent use of tabs and spaces in indentation, so if you don't get that error when running under 3.4, it's probably not a problem of tab/space mixing.