pycharm convert tabs to spaces automatically

PythonPycharm

Python Problem Overview


I am using pycharm IDE for python development it works perfectly fine for django code so suspected that converting tabs to spaces is default behaviour, however in python IDE is giving errors everywhere because it can't convert tabs to spaces automatically is there a way to achieve this.

Python Solutions


Solution 1 - Python

Change the code style to use spaces instead of tabs:

spaces

Then select a folder you want to convert in the Project View and use Code | Reformat Code.

Solution 2 - Python

This only converts the tabs without changing anything else:

Edit -> Convert Indents -> To Spaces

Solution 3 - Python

For selections, you can also convert the selection using the "To spaces" function. I usually just use it via the ctrl-shift-A then find "To Spaces" from there.

Solution 4 - Python

PyCharm 2019.1

If you want to change the general settings:

Open preferences, in macOS ; or in Windows/Linux Ctrl + Alt + S.

Go to Editor -> Code Style -> Python, and if you want to follow PEP-8, choose Tab size: 4, Indent: 4, and Continuation indent: 8 as shown below:

enter image description here

Apply the changes, and click on OK.

If you want to apply the changes just to the current file

Option 1: You can choose in the navigation bar: Edit -> Convert Indent -> To Spaces. (see image below)

enter image description here

Option 2: You can execute "To Spaces" action by running the Find Action shortcut: A on macOS or ctrlA on Windows/Linux. Then type "To Spaces", and run the action as shown in the image below.

enter image description here

Solution 5 - Python

ctrl + shift + A => open pop window to select options, select to spaces to convert all tabs as space, or to tab to convert all spaces as tab.

Solution 6 - Python

ctr+alt+shift+L -> reformat whole file :)

Solution 7 - Python

For me it was having a file called ~/.editorconfig that was overriding my tab settings. I removed that (surely that will bite me again someday) but it fixed my pycharm issue

Solution 8 - Python

Just ot note: Pycharm's to spaces function only works on indent tabs at the beginning of a line, not interstitial tabs within a line of text. for example, when you are trying to format columns in monospaced text.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVaibhav MishraView Question on Stackoverflow
Solution 1 - PythonCrazyCoderView Answer on Stackoverflow
Solution 2 - PythonrunDOSrunView Answer on Stackoverflow
Solution 3 - PythonMuhammad Lukman LowView Answer on Stackoverflow
Solution 4 - PythonlmiguelvargasfView Answer on Stackoverflow
Solution 5 - PythonRaviView Answer on Stackoverflow
Solution 6 - PythonAurel CureaView Answer on Stackoverflow
Solution 7 - PythonKen MyersView Answer on Stackoverflow
Solution 8 - PythonLogan BenderView Answer on Stackoverflow