Notepad++ on windows10 showing whitespace wrong - python

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

Related

Pycharm sometimes does not indent the proper amount

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.

IndentationError when adding new line inside code [duplicate]

I am new to atom, so I opened my existing code using atom and modified few lines, then when I tried running the code with python, I get the following error:
IndentationError: unindent does not match any outer indentation level
I realized that Atom editor does indent my code differently to what I had. refer to the attached picture below showing the different indentation styles. line 1300 is the old indentation and 1301 is the one created by Atom
How can I fix this without modifying my 1000+ line code and so that atom uses the same style of indentation.
You have mixed tabs and spaces in your code. You should use spaces, always.
You can use this plugin to quickly fix your code, and please, use only spaces and 4 spaces for each level of indentation.
I had a similar error while using Atom,I fixed it using below steps.
Install notepad++
Open the file which has issue(one you have mentioned in question) in notepad++.
Go to View > Show Symbol > Show All Characters,this will show up where the tabs and where spaces are available.
Go to Edit->Blank Operations->TAB to Space to replace all tabs with spaces.
Go to View > Show Symbol > Show All Characters,confirm all tabs are replaced with spaces.
Save file and reload page,this will fix this issue.
In the newer versions of Atom, just go to settings > Editor; then scroll down to tab length and change it to 4; then change tab type to soft. This will make the tab key insert 4 spaces instead of a tab character moving forward. You still have to update your previous code using one of the above methods.
In the Atom Text Editor's top menu bar :
click the Packages tab
click Whitespace in the dropdown menu
Choose your preferred option (ie: Convert Spaces to Tabs)
And that will fix this pesky problem.

Convert .py files to correct encoding for Python 3

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

How to overcome Unindentation error in python?

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?

Python indentation borked

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.

Categories