I have come acros the following error many times. Can anyone help me what to do when this error comes up?
Here I am attaching a screenshot.
I use EditorConfig to solve the problem in those languages.
In .editorconfig, you can write the some rules for python
# 4 space indentation
[*.py]
indent_style = space
indent_size = 4
and save the .editorconfig in the directory.
It once happened to me when I - in order to make things shorter - cut and paste some sentences from another source. I had to rewrite them again and everything went fine. Obviously, Python is quite serious about indentantions.
I normally do "select all" in my text editer, then indent, then unindent. The editor should convert all tabs to spaces (or spaces to tabs depending on the setting in your editor.
Edited to address Steven's comment: If you are using Notepad or some such you could do a find/replace for tabs or something?
Related
I have tabs and indents|indent set to 2:
However the actual indentation is coming up as 3 - specifically inside the groupby function - which I have unindented and re-indented several times to be sure the behavior were consistent[-ly incorrect]:
Note that I have also tried the Auto-indent (Option-Command-I) - it also indents stuff to 3 spaces instead of 2. Bit strange..
Any thoughts?
The first thing you might want to check is to make sure you don't have Detect and use existing file indents for editing enabled in Settings/Preferences | Editor | Code Style. If you do, when you auto-indent, it might apply the wrong settings.
You can try to fix incorrectly formatted code by using Option+Command+L (Mac) / Control+Alt+L (PC), which is the Reformat Code option. That should fix the code and then auto-indent should work correctly.
This is most annoying problem in pycharm. Here is simple answer to this :
Just follow the path :
GO to Edit->Convert Indents->To Spaces
This will actually insert space instead of tabular character in empty space when you default enter for next line.
I just pulled from a git repo where the users are on Python 2. My system is running Python 3 and with no changes in the code, I am getting this error:
TabError: inconsistent use of tabs and spaces in indentation
It appears that the solution is to change the char set encoding of the .py files, but working in emacs, I'm not clear how to do this. I'm seeing these instructions:
https://www.emacswiki.org/emacs/ChangingEncodings
but I don't understand how to apply these for utf-8. I'd appreciate any suggestions.
Exists a command untabify:
Convert all tabs in region to multiple spaces, preserving columns.
If called interactively with prefix ARG, convert for the entire
buffer.
I.e. call it with C-u to convert all TABs in buffer.
As comment points out correctly: tabify does the inverse, converts multiple spaces to tabs - while using spaces seems a common convention not just in Python.
This is not a python 2/3 issue, it looks like something in that git repo has wrong indentation. The easiest fix would be to replace all tab characters in all the files with spaces using something like sed
I am getting indent errors all the time because np++ is displaying whitespaces wrong.
Here is picture of np++ compared to editing from terminal(look whitespaces below save function):
Any ideas what might be wrong ?
Edit: Picture showing tabs, spaces...
Thanks to all tips, #Gall was right in comments it was tabs/spaces issue and it was solved by going to settings->Preferences->Tab Settings
And choosing Python + Replace by space
You should also change the tab size of nano then:
Edit your ~/.nanorc file (or create it) and add:
set tabsize 4
Or to convert them to spaces:
set tabstospaces
I am writing a python script using version 2.7.3. In the script a line is
toolsDir = 'tools/'
When I run this in terminal I get SyntaxError: invalid syntax on the last character in the string 'r'. I've tried renaming the string, using " as opposed to '. If I actually go into python via bash and declare the string in one line and print it I get no error.
I checked the encoding via file -i update.py and I get text/x-python; charset=us-ascii
I have used TextWrangler, nano and LeafPad as the text editors.
I have a feeling it may be something with the encoding of one of the editors. I have had this script run before without any errors.
Any advice would be greatly appreciated.
The string is 'tools/'. toolsDir is a variable. You're free to use different terminology, of course, but you'll end up confusing people trying to help you. The only r in that line is the last character of the variable name, so I assume that's the location of the error.
Most likely you've managed to introduce a fixed-width space (character code 0xA0) instead of an ordinary space. Try deleting SP=SP (all three characters) and retyping them.
Try running the code through pylint.
You probably have a syntax error on a nearby line before this one. Try commenting this line out and see if the error moves.
You might have a whitespace error, don't forget whitespace counts in python. If you've mixed tabs and spaces anywhere in your file it can throw the syntax checker off by several lines.
If you copied and pasted lines into this from any other source you may have copied whitespace in that doesn't fit with whichever convention you used.
The error was, of course, a silly one.
In one of my imports I use try: without closing or catching the error condition. pylint did not catch this and the error message did not indicate this.
If someone in the future has this triple check all opening code for syntax errors.
I saw that there are similar titles to this. But my case seems a little weirder. I somehow used a mixture of PyCharm and Vim (and within Vim I have tabstop=4 and shiftwidth=2), and my Python code seems un-fixabl-y borked, indentation-wise. I first saw that in Vim everything was mis-aligned, so I re-aligned everything; but then when I run it I get an error that there's an unexpected indentation, even though in Vim everything seems perfectly aligned. Here's an example (this is how it looks like in Vim):
for f in files:
for line in f:
items = line.strip().split()
items = items[2:]
items = ' '.join(items).split(', ')
When I run it, I get:
File "getEsSynonymLSAVectors.py", line 136
items = items[2:]
^
IndentationError: unexpected indent
I used PythonTidy, I used reindent, I tried :retab, I tried manual re-aligning - nothing seems to fix this. Any experiences/ advice will be appreciated.
Python treated a tab as 8 spaces by default, if you get indentation borked, you'll generally want to switch the tabs to spaces (or vice versa, but I generally find that spaces are easier to deal with). So make sure to set vim to show tab as 8 spaces wide (:set ts=8), to see what python sees.
To fix tab errors in vim, I usually do the following, first I need to be able to see the tabs, so I enabled highlight search (:set hlsearch) and search for tabs (/\t). Then I eyeball the areas that needs to be retabbed. Next, I try to find the right vim tab width setting for the file (:set ts=n and vary n until everything looks good), enable expand tab (:set et), then run the automatic tab fixing (:retab). When all else fail, retab manually.
If you're using version control, make sure to diff with the files before the changes and manually check that you didn't introduce a bug because of unintentional changes in the indentation level. If you don't use version control, keep a backup and run diff on the files.
Try something like this.
First set appropriate settings.
Always use 4 spaces. So change it to tabs = 4 spaces.
First convert all spaces to tabs.
And then convert all tabs to spaces.
(I use Geany)
It has worked for me before many times.