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
How do you format python code in Visual Studio Code?
Asked Answered
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 link –
Wesson
Updated link: marketplace.visualstudio.com/items?itemName=ms-python.python –
Heidelberg
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/11107541 –
Heist
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.
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 you –
Apodal
autopep8 is listed as a requirement. On mac, using brew, you can install it as follows:
brew install autopep8
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 on –
Apodal 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.
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
© 2022 - 2025 — McMap. All rights reserved.