Format Python code ON SAVE in Visual Studio Code

preview_player
Показать описание
With a single press of CTRL+S, you can fully format your code into a beautiful masterpiece. I can't even begin to tell you what this has done for my productivity (like, genuinely).



If you enjoy my content, consider supporting me on Patreon or becoming a member!

If you need help with anything, feel free to join the Discord server:

I get a lot of people asking, so here's my Visual Studio Code setup!



If you have any questions, don't hesitate to ask in the comments! I'll try and answer as soon as I can, providing someone else hasn't already done so.

#ide #editor #vscode
Рекомендации по теме
Комментарии
Автор

I have searched for a lot of tutorials before, but none of them worked. However, this video solved my problem. Thank you very much.

xiaoer-nqpk
Автор

Thanks!! Thats what I was really looking for

FelipeTaranto
Автор

That's exactly what I was looking for. Thanks!! ❤

cindinishimoto
Автор

thank u so much mate, really helped me out

nimamasoumi
Автор

Very Interesting, happy to discover this

kajairokajairo
Автор

sir, python formatter is not working in my vscode? when i given a extra space but didn't work for this..what i should do?

picchiscam
Автор

Thank you for this. Could you also share how to configure black in vscode 80 character length. Also help understanding the difference between the default setting of balck

tridibbiswas
Автор

Is there a way how I can mark a piece of code to be immune to the formatting by Black? Sometimes I want a section of code to look a certain way for readability or what have you.

crescentfuze
Автор

For indenting, I prefer tabs to spaces. So i disable auto-converting tabs to spaces. I discovered that black doesn't allow me to disable converting tabs to spaces. So i won't use black.

johnaweiss
Автор

For some reason it was not working for me, but I found out that adding only this part made it work:

"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true
}

wehbe
Автор

I have this 😂 but configured with Prettier but for JavaScript I'm gonna do it for python too 👌🏼

xtz_
Автор

not using isort, only black, sadly it does nothing. It also doesnt help if I try the default formatter "python" no matter what, nothing happens

thegymteachersgrandma
Автор

I got black installed without manually editing the json.
Open a Python file in VS Code.
Right-click on the editor to display the context menu.
Select Format Document With....
Select Configure Default Formatter... from the drop-down menu.
Select your preferred formatter extension from the list.

I noticed this added your config statements to the json.

johnaweiss
Автор

Hello Sr, first of all, thanks a lot for the video.
I am new in Python and coding, just trying to learn. I have spent the entire day trying to mimic youtube videos from scratch and nothing seems to work as shown in the videos lol

I installed all extensions (Python, Black and Isort) and followed all instructions exactly as you has shown in the video, however when I got to the settings and I clicked on the setting.json view I got a very short code, nothing similar to yours. For instance, in my settins.json code there were no python black or isort section. The code was pretty much this:

{
"workbench.colorTheme": "Default Dark Modern",
"editor.fontSize": 16,
"editor.formatOnSave": true,
"editor.defaultFormatter": "ms-python.autopep8",
"[python]": {
"editor.defaultFormatter": "ms-python.autopep8"
},
"editor.fontFamily": "'FiraCode Nerd Font', monospace"
}

What am I doing wrong? :/

rafafazzi
Автор

"source.organizeImports": true

true is underlined in yellow

problem: Incorrect type. Expected "string".

what to do bro?

kuntaldey
Автор

Doesn't work for me. It doesn't format with Ctrl + Shift + F (it's working for JS) and it doesn't work on save

nicko
Автор

too tiny, cant see. I'm in settings.json, and i don't see any of that. i have only 10 lines of json.

johnaweiss
Автор

Assuming I am not the only one looking for a copy and paste ... here are the settings
{
"[python]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
},
"isort.args": ["--profile", "black"],
"python.linting.mypyEnabled": true,
"numpy",
false,
"window.zoomLevel": 3,
}
i was looking for something to fix indentenation but this sadly did not do it. I swear if I had money, I would pay somebody to make a fork to just freaking use brackets. they work so much better.

NicaSimon
Автор

"source.organizeImports": true gives error, saying it expects string

warriorgirl