In Pycharm I keep running into this error:
TabError: inconsistent use of tabs and spaces in indentation
I know its a problem with tabs/spaces.
I want:
if len(myresults)==0:
print("TEST")
Whenever I type, pressing enter after every line typed I actually type:
if len(myresults)==0:
print("TEST")
Causing this error. How do I fix it? Here are my setting s for pycharm:
Pycharm Settings
I'm probably missing something obvious, but I simply cannot find it.
Try,
In the "Settings | Editor | Code Style" -- try disabling "Detect and use existing file indents for editing" in case if you have it enabled (it is by default). NOTE: re-opening file in editor may be required.
Do you have any .editorconfig files anywhere in the path of that file? Settings from .editorconfig have priority (will overwrite) over your IDE settings.
Go to Settings --> Editor --> Code Style --> Python --> Tabs and Idents.
Here activate (checkbox) Use tab character AND Smart tabs
Thats works for me.
What worked for me:
"Settings | Editor | Code Style" -- disable "Detect and use existing file indents for editing" (as #Nipun Sampath suggestted)
Together with:
"Settings | Editor | Code Style | Python" -- disable "Use tab character"
For reformatting the file: Ctr+Alt+L -> 'Code cleanup' on either selected text or whole file (depending on what is needed)
I'm using Pycharm and Jupyter Notebook and had the same problem with both of them. I could not fix it with "convert Indents", So I uninstalled some of the modules that I was using in my programm and reinstall them and worked for me.
I'm using Pycharm 2019.1. For me, this error constantly appeared always I pressed Enter to write a new line, and I have to manually rewrite every indentation in order to disappear
the red sub-lines that indicate error. I fixed it by analyzing the full code into another text editor (kate editor in my case, but you can use another one). I verified there's some indentations written as [TAB] and the most of them written as four simple spaces. So I replaced all indentations written as [TAB] by indentations written as four spaces (most of the editors replace by using [Ctrl R] shortcut) and... voilà. Everything works fine.
Note: I couldn't do the replace into Pycharm editor itself. Apparently pycharm editor does not differentiate a [Tab] of four spaces when you try replace by [Ctrl R].
Hope it help future users.
Related
I'm trying to format my code with autopep8 (because this appears to be the default one in VSCode). But there is a problem with it:
I am using a lib that doesn't care about typing, so I need to suffix some lines with # type:ignore to make the linter ignore that particular file. But when formatting (with autopep8) the formatter moves the comment to the line above. Then my linter doesn't ignore the line like it should.
So with e.g. this code:
somevar = dumblib.foobar() #type:ignore
I hit Shift+Alt+F to format, and then the code is formatted to this:
# type:ignore
somevar = dumblib.foobar()
And then, the linter complains that the type of "somevar" is unknonwn.
How can I prevent autopep8 from moving the comment during a reformat?
It's a relatively old question but in case others also struggle with this, I will leave a solution that helped me.
The autopep8 rule that is responsible for moving comments above the code line is nothing but a max-line-length. I was able to fix this by increasing the maximum line length. To do this I created a configuration file in my project (.pep8) and configured vscode to use it (in the autopep8 extension settings).
Configuration file contents:
[pep8]
max-line-length = 160
When I type the python code in vs code, indent tab size is 2. But when I save the file, tab size is changed to 4. I think this is because of autopep8.
How can I change the tab size, when I save the python code?
※ I already change editor.tabsize to 2
It's recommended to set '--indent-size' to change the autopep8 behavior. You can refer to this page for details. But I can't make it works in this way, so I directly
make a change in autopep8.py(under site-packages which you installed the autopep8) file, at the line of 112 change "DEFAULT_INDENT_SIZE = 4" to "DEFAULT_INDENT_SIZE = 2". It works and it's the same to set '--indent-size' in logic.
You will need to configure autopep8 to keep indent to two spaces. That way it won't undo how VS Code is inserting spaces for you.
I want to introduce a tab character in the new line for creating a new block in vim for python files.
Example
when I write
if(a < 5):
and press Enter, the cursor should come in next line with a tab after if in vertical alignment from the above line. Something like this
if(a < 5):
a = 5
I have configured my .vimrc file like this
set nu
set autoindent
set tabstop=4
syntax enable
set showmatch
colorscheme gruvbox
set bg=dark
"automatically creates a block after :
autocmd FileType python inoremap :<CR> :<CR><Tab>
Note:
1) Last line, I have written for the intended purpose mentioned in my question.
2) autoindent is set.
3) I dont want to use any plugin.
Problem I am facing:
When I press Enter after : , in addition to tab, one extra space is also created which creates wrong indentation.
Thanks in advance for helping
The way you're trying to do this is the hard way. There's a filetype plugin and a Python syntax type and it already knows what to do.
filetype plugin indent on
And don't use set autoindent. It's not very intelligent, it just copies the indent from the previous line. It's the opposite of what you're trying to do.
Files ending in .py should automatically get the right settings. If you need to force the current syntax type in a buffer, you can do that with
:set filetype=python
If you want to use tabs instead of spaces,
" Globally
set noexpandtab
" Python only
autocmd FileType python set noexpandtab
However, I'll note that using tabs in Python is not recommended and goes against published best practices (see PEP 8).
Lastly I'll mention that "plugin" is just a Vim term. All of this ships with Vim by default, this does not create a third-party dependency.
I am using Spyder IDE to develop code and the IDE currently has default end-of-line character set to be CRLF. I would like to use '\n' instead because all my existing source code uses '\n' and so I don't want to have a bunch of new files that use a different end-of-line character.
Is there a way to specify the default end-of-line character in Spyder IDE? If yes, how?
(Spyder maintainer here) To configure the end of line characters you want to use, you need to go to the menu
Tools > Preferences > Editor > Advanced settings
then to the section End of line characters and select the ones that Spyder will use on save.
I just started exploring Spyder 4.1.5 (unfortunately could not get 5.1.5 installed), and found this for EOL settings:
'Tools' > 'Preferences' > choose 'Editor' in the list on the left > 'Advanced setting' tab on the right > at the bottom is the 'End-of-line characters' section.
To convert existing CRLF to LF, select the LF option in the dropdown, then for each file you want to convert, make any edit and save the file and voila. (I think you have to do the edit+save step file-by-file.)
As Carlos Cordoba wrote, Spyder does not allow setting the line ending but it does respect the line endings of a file.
If a file has LF line endings and you hit enter, it wont add CRLF even if you are on windows.
My solution is to change the line endings of a new file with notepad++ (edit-> EOL conversion)
For many existing files you can use the replace function, explained here
When I edit a python file in Vim (using MacVim), and I press o to insert a new line, Vim throws the following errors:
Error detected while processing function <SNR>20_CheckAlign..GetPythonIndent:
line 30:
E121: Undefined variable: dummy
Press ENTER or type command to continue
Error detected while processing function <SNR>20_CheckAlign..GetPythonIndent:
line 30:
E15: Invalid expression: line('.') < 7 ? dummy : synIDattr(synID(line('.'), col('.')
, 1), 'name') =~ '\(Comment\|String\)$'
How do I fix this?
I figured out the problem. It was throwing an error whenever the file's tab settings were different from the editor's tab settings. For example, my test.py file was set to 2 spaces per tab, with tabs expanded into spaces, whereas my editor was set to 4 spaces per tab, no expand.
So the solution workaround was to set Vim's tab settings to the settings of the python file being edited.
Use the following modeline in your python files, that its tab settings are consistent.
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
Alternately, you have them set in your .vimrc file too.
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
These are minimal set of things which you ensure consistency when working with python file.
There are some great vimrc examples available which you can use as well.
Changing the indentation settings did not work for me so I worked around this by modifying the python indentation file (/path/to/vim/indent/python.vim).
In the GetPythonIndent function I simply replaced all instances of dummy with 0. This fixed the problem for me.
Alternatively you could just set s:maxoff to something ridiculously high but this is somewhat less elegant.
To resolve the problem it seems one must edit the function GetPythonIndent() in /path/to/vim/indent/python.vim.
To do this, one must read in the help docs, user_30, indent, indentexpr, and indent-expression (also, C-indenting contains general info that will help explain what's going on).
The simplest solution therefore is to switch indenting off when you are editing a python file, until such time as you have time to deal with this irritating bug:
:set indentexpr=""
You will see in /path/to/vim/indent/python.vim that expression is set to run the buggy function GetPythonIndent:
setlocal indentexpr=GetPythonIndent(v:lnum)
Set it to "" there to save the inconvenience of unsetting it every time you open a python file.
Dani's answer above looks like it might be the better solution, since it would preserve some form of auto-indentation. But this will give you instant relief and save having to read and understand the buggy func.