139

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.

Vaibhav Mishra
  • 11,384
  • 12
  • 45
  • 58

8 Answers8

140

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.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • 37
    this was my default setting however it seems like there was some bug in the IDE which was not working so what I did is Edit>Convert Indent>Spaces – Vaibhav Mishra Aug 05 '12 at 13:05
  • 3
    Python should have 4-space indentation. – R891 Jul 06 '15 at 19:19
  • 3
    This answer doesn't work for me. Not sure why it's accepted, seeing as it doesn't work for OP either. Follow up question: https://stackoverflow.com/questions/47102828/is-it-possible-in-pycharm-to-map-a-tab-to-produce-4-spaces-if-so-how – Stefan Monov Jan 21 '18 at 11:50
  • 4
    In my case I had to disable Editor Config under Editor -> Code Style -> Enable Editor Config Support, because I unknowingly had a `~/.editorconfig` file overriding PyCharm's settings. – Elliott Beach Jan 21 '18 at 14:03
  • This sets the default. It does NOT alter tabs or spaces in an existing file. See the answer below from @LowKianSeong for a solution that addresses the question. – Zim Jul 30 '18 at 20:45
  • 6
    You can force it to use spaces even for existing files by unchecking "Detect and use existing file indents for editing" under the general "Code Style". After that, typing tab on an existing file will immediately insert spaces rather than tabs. – David Kinghorn Oct 29 '19 at 16:46
  • I had to check and uncheck the checkbox to make it work. – Pierre Thibault Mar 20 '20 at 16:33
89

This only converts the tabs without changing anything else:

Edit -> Convert Indents -> To Spaces
runDOSrun
  • 10,359
  • 7
  • 47
  • 57
  • 1
    Hey, this should be the accepted. It works locally. E.g. useful for Makefile that need to have tab as indentation – swdev Aug 22 '18 at 09:48
63

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.

Muhammad Lukman Low
  • 8,177
  • 11
  • 44
  • 54
31

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

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
14

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.

Ravi
  • 216
  • 3
  • 11
7

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

Aurel Curea
  • 71
  • 1
  • 1
  • 1
    to tabs didn't work to convert an existing block of code. This is the only solution that worked for me. Thanks! – Jed Aug 15 '19 at 06:45
1

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

Ken Myers
  • 596
  • 4
  • 21
1

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.

Logan Bender
  • 367
  • 1
  • 5
  • 12