Search code examples
pythonidespyder

How do I beautify (auto-format) a piece of code in Spyder IDE


How can I select a piece of code and apply auto-format, aka beautification to it?

I especially mean correctly indenting lists broken into several lines, parentheses and square brackets.

Auto-indent does nothing.


Solution

  • Nowadays (or at least in Spyder v5), Spyder includes autopep8 directly in the GUI and has an option of automatic formatting (as requested in the original question).

    Simply go to Tools > Preferences then choose Completion and linting > Code style and formatting. There, toggle on Enable code style linting and Autoformat files on save.

    Now when you save your file, Spyder will beautify your code, when it can infer what to do. This should include adding missing whitespaces after commas or around operators, removing whitespaces in empty lines or missing new lines to separate functions, just to name a few.