Why do i get this Python indentation error? - python

I have a indentation error in Python and i dont know why and how to resolve it.
I looked for special characters that i might have missed, but none found;
Why do i get this error ?
user = {'_id':username}
users.find_one(user)
IN CONCLUSSION : NEVER MIX TABS WITH SPACES!

The most common reason for this type of error is mixing tabs and spaces.
To fix it try converting all tabs to spaces in your file.
From PEP 8 -- Style Guide for Python Code:
Tabs or Spaces?
Never mix tabs and spaces.
The most popular way of indenting Python is with spaces only. The second-most popular way is with tabs only. Code indented with a mixture of tabs and spaces should be converted to using spaces exclusively. When invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!
For new projects, spaces-only are strongly recommended over tabs. Most editors have features that make this easy to do.

you should check for tab and space mixes:
python -tt blog.py

Tabs mixed with spaces is a common cause of this issue. Try reformatting your document which should help fix the issue.

Related

How to manage 'inconsistent use of tabs and spaces in indentation' across editors

I've been working with python for a while, and understand how to use tabs correctly, but recently in a project I have started working across Jupyter notebooks and Sublime. When I copy and paste code over, the indentation gets completely messed up. Usually it goes from tabs to 4 spaces and it doesn't like that so I have to manually change every line. It happens on occasion when I copy from one python file to another in just Sublime as well.
Is there some sort of general way, or just setting within those applications, that can prevent this?
Jupyter Notebooks indent with 4 spaces by default. Not sure where you'd be getting tabs unless you originally copy-pasted tabs.
If you want to keep the tabs in SublimeText, select a Python file, go to "Preferences" --> "Settings - Syntax Specific" and add the line:
"translate_tabs_to_spaces": false
Although, if your sources were already mixed (some cells in jupyter use tabs, some use spaces) then it's better to standardise on paste in sublime, and use:
"translate_tabs_to_spaces": true
Also, when you're pasting in SublimeText, instead of just Paste with Ctrl+V, use Paste & Indent: Ctrl+Shift+V
Btw, Python3 recommends 4 spaces by default, almost as a standard. So maybe set that in your editor. Most of them have settings for "Tabs & Indents" somewhere with code formatting.

Python Tidy Code

I edit all my Python files on a remote Centos server using nano. This seems to work well. Occasionally I need to make a small change that changes the indention over the entire file. Is there an easy way to convert all the one space indents to 4 space etc? I have looked at PythonTidy.py but it seems to change too many things.
Not sure if you would consider switching editors. You can customize VIM so it handles auto indentation and easily set tabs to spaces vice versa using the vimrc file. Vim also has a convenient fix indentation command for your issue.
In you're favorite text editor, use search and replace. Find all the instances of
"\n [^-\s]"
and replace them with
"\n "
As long as it takes the newline and regex as expected, it'll indent all the newlines followed by a space followed by a not whitespace (meaning, it won't change 4 spaces into 7 spaces).
I found this:
How to fix python indentation
Installed python-tools then used reindent.py. Worked absolutely perfect.
Thanks.

Easy way to set indentation on whole file

I screwed up the formatting on a few files using VIM. What I thought was tabs was something that looked like tabs. Is there an easy to change the formatting back to standard tabs?
I can find a lot of info on doing from tabs to spaces, but not the other way around.
You can do this by disabling expanding tabs to spaces, and than replacing every sequence of whitespace with tabs:
:set expandtab
:%retab!
Note: Retab command takes range, so you can execute it on the part of the file. Also, before executing this command, set proper size of tab, depending on your preferences, for example:
:set tabstop=2
This means that every two spaces will be replaced by tab.

Textmate Whitespace/Invisibles - Show Spaces

Is there a way to show "Soft Tabs" (spaces) in TextMate? View → Show Invisibles works well for keeping track of indentation if you're using tabs for indentation. Unfortunately in languages where indentation is semantic you generally have to use spaces.
(Python, YAML, HAML, CoffeeScript)
Any suggestions for showing this whitespace or keeping track of soft indentation in TextMate? Should I keep holding out for Textmate2?
Alternative strategies and suggestions are also welcome.
The latest version of TextMate 2 highlights spaces when Show Invisibles is enabled.
EDIT:
You can even customize which invisibles to show with which character by modifying the invisiblesMap property in .tm_properties file.
From the TextMate changelog:
This can be set to a string which is used to control which glyphs are used for invisible characters. Add either \n, \t, or a space to the string, followed by the glyph that should be used to render the character, or prefix it with ~ to disable rendering of that character. For example to disable rendering of spaces and render tabs as ┊ add this to .tm_properties: invisiblesMap = "~ \t┊".
Sidenote:
THIS IS NOT THE CASE ANYMORE, functionality has been restored.
According to the Log of 2013-10-23 (v2.0-alpha.9489): "Show Invisibles will no longer treat space as an invisible (which was added in previous build) as it was causing issues with right-to-left rendering and combining marks used after spaces. The feature might be back, but needs to be implemented differently."
You can use soft tabs - as described here. I have also problem to find it when I needed this feature;)

Enforce "spaces" or "tabs" only in python files?

In Python, is there a mean to enforce the use of spaces or tabs indentation with a per file basis ?
Well, perhaps "enforce" is too strong, more like a "recommendation".
I keep receiving patch files with mixed indentation and this is annoying... (to say the least) Python itself can tell when there is a problem, but I am searching something to do that at the editor level, like it exists for the charset.
Edit : Ok, my question wasn't clear, I am asking this because I keep receiving corrections and patches in any mix of tab/space you can imagine. I am using Mercurial as a DVCS, perhaps something exists at this level ?
Tim Peters has written a nifty script called reindent.py which converts .py files to use 4-space indents and no tabs. It is available here, but check your distribution first -- it may have come bundled in an Examples or Tools directory. (On the latest LTS Ubuntu, it is provided by the python2.7-examples package.)
If you can set up a Mercurial hook you should be able to run all files through reindent.py.
By the way, if you are using unix, then your system may also have the expand (and unexpand) command, which convert all tabs to spaces (and spaces to tabs). However, in this case, I think reindent.py is the right tool.
Look at the tabnanny module: — Detection of ambiguous indentation.
This is something your editor should do for you. Most editors (try Notepad++ for example, it's free) will let you set whether hitting tab enters a tab character or a number of spaces. I'd recommend using two spaces instead of tab in all files (I find 4 is too much). Using spaces instead of tabs is better as it means that you can indent stuff using both the space & tab keys without worrying about messing up your files.
If you have files that have a mix it isn't hard to write your own script to convert tabs to spaces
As explicited in PEP 8, never mix tabs and space. However, a file with both may just run...
As it says there:
The most popular way of indenting Python is with spaces only. The
second-most popular way is with tabs only. Code indented with a mixture
of tabs and spaces should be converted to using spaces exclusively.
When invoking the Python command line interpreter with the -t option, it issues
warnings about code that illegally mixes tabs and spaces. When using -tt
these warnings become errors. These options are highly recommended!
the solution is therefore to use as a default:
python -t my_mixed_code.py
To answer at the editor level, this depends on the editor, please specify!

Categories

Resources