Search code examples
pythonvisual-studio-codeformatterautopep8

python formatter (autopep8, paste format) (VSCode)


I'd like to copy and paste some code but which code's format so dirty.

e.g.

n = input()

while True:
    try:
        if (int(n) < 10):
            pass
        else:
            pass
    except:
        break

after paste

n = input()

while True:
    try:
        if (int(n) < 10):
            output = str(n) * 2
        else:
            pass
        
        if (int(n) < 10):
    pass
else:
    pass

    except:
        break

I want

n = input()

while True:
    try:
        if (int(n) < 10):
            pass
        else:
            pass

        if (int(n) < 10):
            pass
        else:
            pass

    except:
        break

Also second one doesn't work formatter like autopep8. I mean autopep8 can change second one to third one.

Is it normal? Even option's are "editor.formatOnSave": true, "editor.formatOnPaste": true

Please help me. Thank you. Have a nice day :)


Solution

  • I fell in love with https://pypi.org/project/black/, which is automatically formatting your code in your editor right before saving it. Simple to install, no maintenance. Just look up how to install it to your editor.