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

4

17

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.

Farouche answered 31/5, 2016 at 10:48 Comment(0)
A
17

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.

Aleksandropol answered 14/2, 2022 at 13:1 Comment(0)
F
6

One possible solution is to use this plugin: autopep8 for Spyder

Although since Python is a language where code functionality depends on the amount of whitespace, beautification tools can never really format the code in an unified way.

Unfortunately, autopep8 does not solve code formatting conventions that are violated (as indicated by Pylint).

Farouche answered 8/6, 2016 at 18:1 Comment(0)
H
3

ctrl + alt + l or right click and "format line..."

Heckman answered 8/6, 2022 at 11:11 Comment(0)
S
1

You may want to also check out Black

Shirt answered 27/3 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.