How do you format python code in Visual Studio Code?
Asked Answered
W

5

18

Opened a ticket with AutoPep8, but when I installed the autopep8 extension, it doesn't seem to find the module. When I change the config to tell it where it is a format doesn't do anything. Formating in terminal does however. Any ideas? https://github.com/hhatto/autopep8/issues/349#issuecomment-335662565

Wesson answered 11/10, 2017 at 3:9 Comment(8)
Can you show a picture of what is happening? What are you wanting from the formatting?Heidelberg
Have you tried the default Python extension yet?Heidelberg
Got a 404 on that linkWesson
Updated link: marketplace.visualstudio.com/items?itemName=ms-python.pythonHeidelberg
I installed that and I get the same result. Also it warns that its not a recommended interpretor.Wesson
Could it be that you are using a different python installation on the command line vs in VS Code?Scherzo
I’m using the “python” extension as linked above. I also have python normal installation on my computer.Wesson
related: code.visualstudio.com/docs/python/formatting and https://mcmap.net/q/500863/-vs-code-python-extension-circa-v2018-19-no-longer-includes-support-for-linters-and-formatters-why/11107541Heist
S
17

Install the below python packages for Python Code Format supporting in Windows

pip install autopep8

pip install pylint

Once you have successfully installed the packages, Open the code in vs code -> Press Ctrl + A and Ctrl + K, it will format the code.

Supernova answered 27/3, 2020 at 4:42 Comment(1)
This did not work for me. However, after running this command, I also went to the extensions of VSCode and installed the "autopep8" extensions. I then updated my user settings json file with "[python]": { "editor.defaultFormatter": "ms-python.autopep8"} and now VSCode is formatting my python code. This is on a mac, if that matters to youApodal
C
7

autopep8 is listed as a requirement. On mac, using brew, you can install it as follows:

brew install autopep8
Cadelle answered 22/6, 2018 at 21:21 Comment(2)
The other answer recommends pylint, is it needed for Mac to autoformat code?Cantata
This did not work for me. However, after running this command as well as the commands of pip install autopep8 and pip install pylint I installed a VSCode extensions called "autopep8". I wrote more specific instructions for making it work in a comment to an answer by @Dhandapani Sudhakar on this page you're currently onApodal
S
4

you should try black.

pip install black

and then open the auto format option in vscode when save and modify the python format provider to black in vscode settings. enjoy the black.

Sparks answered 13/8, 2022 at 14:59 Comment(0)
A
1

after installation press ctrl + shift + F

Aitchbone answered 27/5, 2024 at 13:57 Comment(0)
H
0

You can use Ruff, it is suggested in the official VSCode documentation. Among their suggestions, it is the highest-rated community extension. Once installed, simply press

(CTRL or COMMAND) + ⇧ + i
Hydrometeor answered 30/4, 2024 at 12:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.