Search code examples
pythonshellpython-idle

Is there any way to set Python IDLE's interactive shell indentation width to 2 spaces instead of its default?


I'm not asking about Options > Configure IDLE > Fonts/Tabs > Indentation Width and setting that to 2. This only sets the indentation width within a file and not the indentation width for the interactive shell.

What Python IDLE file do I need to change to get 2-space spacing in the interactive shell?

I like to code with 2 spaces instead of 4 so not having the interactive shell also indent by the same spacing slows me down when transferring out of shell and into a file for example.


Solution

  • There is a solution!!! *1

    Solution

    go to idlelib (/lib/idlelib/) *2
    open "pyshell.py"
    go to class PyShell, __init__

    there are 3 lines

    self.usetabs = True
    # indentwidth must be 8 when using tabs.  See note in EditorWindow:
    self.indentwidth = 8
    

    just change them to

    self.usetabs = False
    # indentwidth must be 8 when using tabs.  See note in EditorWindow:
    self.indentwidth = 4 # you decide, lot of people prefer 2
    

    P.S.

    this will result in wierd looks:

    >>> def f(x): # calculate x*abs(x)
        if x > 0: # it doesn't look it, but this is indented once
            return x*x # and this twice
        else:
            return -x*x
    

    But I think it looks better then 8-spaces-tab indent

    footnotes

    *1 I'm working with python 3.8 on windows 10, but this may work elsewhere

    *2 depending where python is installed
    step 1: find python (to me it is "C:\Programs\Python 3.8")
    step 2: from there go to /lib/idlelib/