Format Python code ON SAVE in Visual Studio Code
Вставка
- Опубліковано 5 жов 2024
- 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!
patreon.carber...
join.carberra.xyz
If you need help with anything, feel free to join the Discord server:
discord.carber...
I get a lot of people asking, so here's my Visual Studio Code setup!
• My Visual Studio Code ...
-
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.
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,
"autoDocstring.docstringFormat": "numpy",
"terminal.integrated.enableMultiLinePasteWarning": 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.
I believe there is one called Bython!
This is extremely useful! Thanks!!!!
You're welcome!
Thanks!! Thats what I was really looking for
game changer - this will save so much time
Very Interesting, happy to discover this
Glad you enjoyed, and thanks for becoming a member!!
thank u so much mate, really helped me out
thanks man! you saved my life.
That's exactly what I was looking for. Thanks!! ❤
You're very welcome!
Thanks for sharing this!
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.
thanks
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?
he don't reply to issues.. only wants to hear whats works 😅😅 unsubscribed
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.
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
}
thank you its really helpful
This is useful, thank you
Thanks man it helped me
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? :/
Mine will be significantly longer as I have a tonne of settings in there. You'll need to update the settings.json file manually, specifically the bit about the default formatter.
Thank you!
Thanks so much!
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
I should really make a proper video about black. You can use a command line option (black -l 80), or set it in the pyproject.toml. Black's default line length is 88.
@@Carberra Thank you. Would really appreciate it. Also it would be nice if you could do a video on pyproject.toml - the basics - what it is , how to configure and do's and dont's
That's a good idea!
I've just realised I'm an idiot -- I *have* done a video on Black! ua-cam.com/video/A6S2nZAXgT8/v-deo.html
GOD BLESS YOU!
Thanks
"source.organizeImports": true
true is underlined in yellow
problem: Incorrect type. Expected "string".
what to do bro?
Same, did you find any solution?
Thanks keep up and upload more please
Liked the theme, which is it? :)
Info's in the description 😄
I have this 😂 but configured with Prettier but for JavaScript I'm gonna do it for python too 👌🏼
Oh nice, didn't know JS has similar tools. I guess it only makes sense though.
@@Carberra yes it's amazing u should try it
If I ever have to use JS I will 😆 Though actually for my work I will have to at some point.
@@Carberra I started working as A junior Frontend developer I use just Js and Ts all day 😂. And we study React in the UN
perfect
super sir
thanks
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
Your config might be slightly wrong -- it can be quite fiddly. If you like, send over your config and I'll take a look.
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
from the lower right corner chose your environment , play with them it should work
Shot in the dark here.. Does Svk say you anything 😂😂
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.
You can use "# fmt: off" and "# fmt: on" to do that. Wrap that block of code in those comments and it'll stay as is.
@@Carberra Awesome. Thank you.
too tiny, cant see. I'm in settings.json, and i don't see any of that. i have only 10 lines of json.
If you're on mobile, you should be able to zoom in to get a better view. If you still can't see, my config is (I think) completely stock, so just copy the examples on the extension install page.
"source.organizeImports": true gives error, saying it expects string
Change it to: "explicit".
thanks
thanks