Format Python code ON SAVE in Visual Studio Code

  Рет қаралды 47,360

Carberra

Carberra

Күн бұрын

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

Пікірлер: 69
@Carberra
@Carberra 2 ай бұрын
This has changed a little: to organise imports, source.organiseImports should now be "explicit" rather than true.
@xiaoer-nq3pk
@xiaoer-nq3pk Жыл бұрын
I have searched for a lot of tutorials before, but none of them worked. However, this video solved my problem. Thank you very much.
@NicaSimon13
@NicaSimon13 Жыл бұрын
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.
@robertpegg4672
@robertpegg4672 7 ай бұрын
I believe there is one called Bython!
@abidrahim7607
@abidrahim7607 3 күн бұрын
worked flawlessly, thanks!
@juan.o.p.
@juan.o.p. 2 жыл бұрын
This is extremely useful! Thanks!!!!
@Carberra
@Carberra 2 жыл бұрын
You're welcome!
@kajairokajairo2271
@kajairokajairo2271 2 жыл бұрын
Very Interesting, happy to discover this
@Carberra
@Carberra 2 жыл бұрын
Glad you enjoyed, and thanks for becoming a member!!
@johnaweiss
@johnaweiss Жыл бұрын
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.
@manoor0858
@manoor0858 6 ай бұрын
thanks
@FelipeTaranto
@FelipeTaranto 9 ай бұрын
Thanks!! Thats what I was really looking for
@cindinishimoto9528
@cindinishimoto9528 Жыл бұрын
That's exactly what I was looking for. Thanks!! ❤
@Carberra
@Carberra Жыл бұрын
You're very welcome!
@sohrabrs
@sohrabrs 2 ай бұрын
"[python]": { "editor.formatOnSave": true, "editor.defaultFormatter": "ms-python.black-formatter", "editor.codeActionsOnSave": { "source.organizeImports": true } }, "python.linting.mypyEnabled": true, "autoDocstring.docstringFormat": "numpy", "terminal.integrated.enableMultiLinePasteWarning": false, "window.zoomLevel": 0, "isort.args": ["--profile", "black"]
@nimamasoumi646
@nimamasoumi646 6 ай бұрын
thank u so much mate, really helped me out
@MattDeveloper
@MattDeveloper 9 ай бұрын
Thanks for sharing this!
@alantchunay
@alantchunay 9 ай бұрын
thanks man! you saved my life.
@FarazArastu
@FarazArastu 5 ай бұрын
game changer - this will save so much time
@picchiscam7288
@picchiscam7288 Жыл бұрын
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?
@prashantjain1191
@prashantjain1191 Жыл бұрын
he don't reply to issues.. only wants to hear whats works 😅😅 unsubscribed
@binhna
@binhna Жыл бұрын
This is useful, thank you
@moeinzand4800
@moeinzand4800 Жыл бұрын
Thanks man it helped me
@PositiveMentalAttitude284
@PositiveMentalAttitude284 8 ай бұрын
thank you its really helpful
@Acksakal
@Acksakal Жыл бұрын
GOD BLESS YOU!
@hasbealam
@hasbealam Жыл бұрын
Thank you!
@marsdwarf
@marsdwarf Жыл бұрын
Thanks so much!
@enigma_mysterium
@enigma_mysterium 2 күн бұрын
Did not work. I restarted the machine and VS code. Still nothing is formatted when saving a python file in VS Code.
@cabrex5864
@cabrex5864 13 күн бұрын
I have autosave on, how does it work on that
@johnaweiss
@johnaweiss Жыл бұрын
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.
@クバ
@クバ 2 ай бұрын
Put it in the settings.json after installing the extensions: "[python]": { "editor.formatOnSave": true, "editor.defaultFormatter": "ms-python.black-formatter", "editor.codeActionsOnSave": { "source.organizeImports": true } }, "isort.args": ["--profile", "black"],
@tridibbiswas3361
@tridibbiswas3361 Жыл бұрын
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
@Carberra
@Carberra Жыл бұрын
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.
@tridibbiswas3361
@tridibbiswas3361 Жыл бұрын
@@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
@Carberra
@Carberra Жыл бұрын
That's a good idea!
@Carberra
@Carberra Жыл бұрын
I've just realised I'm an idiot -- I *have* done a video on Black! kzbin.info/www/bejne/d2e2Y6GQdr2aipo
@williamyoutube368
@williamyoutube368 Ай бұрын
Hi Sir, I added the line "isort.args": ["--profile", "black"], but it is disabled due to: This setting cannot be applied because it is not registered as language override setting. Could you suggest how to fix? Thanks
@kuntaldey4471
@kuntaldey4471 11 ай бұрын
"source.organizeImports": true true is underlined in yellow problem: Incorrect type. Expected "string". what to do bro?
@kingboy-vh1kd
@kingboy-vh1kd 8 ай бұрын
Same, did you find any solution?
@Face_the_Reality
@Face_the_Reality 2 жыл бұрын
Thanks
@thegymteachersgrandma
@thegymteachersgrandma Жыл бұрын
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
@clanzu2
@clanzu2 9 ай бұрын
from the lower right corner chose your environment , play with them it should work
@orsimhon133
@orsimhon133 10 ай бұрын
Liked the theme, which is it? :)
@Carberra
@Carberra 10 ай бұрын
Info's in the description 😄
@FricoSimon-li6hd
@FricoSimon-li6hd 10 ай бұрын
perfect
@SniperBoy01
@SniperBoy01 Жыл бұрын
super sir thanks
@GamesindieSR
@GamesindieSR 2 жыл бұрын
Thanks keep up and upload more please
@biltongandboba
@biltongandboba Жыл бұрын
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.
@Carberra
@Carberra Жыл бұрын
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.
@biltongandboba
@biltongandboba Жыл бұрын
​@@Carberra Awesome. Thank you.
@rafafazzi7316
@rafafazzi7316 Жыл бұрын
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? :/
@Carberra
@Carberra Жыл бұрын
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.
@wehbe4099
@wehbe4099 Жыл бұрын
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 }
@nicko9958
@nicko9958 Жыл бұрын
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
@Carberra
@Carberra Жыл бұрын
Your config might be slightly wrong -- it can be quite fiddly. If you like, send over your config and I'll take a look.
@johnaweiss
@johnaweiss Жыл бұрын
too tiny, cant see. I'm in settings.json, and i don't see any of that. i have only 10 lines of json.
@Carberra
@Carberra Жыл бұрын
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.
@Anarkhea
@Anarkhea 5 ай бұрын
Shot in the dark here.. Does Svk say you anything 😂😂
@0xtz_
@0xtz_ 2 жыл бұрын
I have this 😂 but configured with Prettier but for JavaScript I'm gonna do it for python too 👌🏼
@Carberra
@Carberra 2 жыл бұрын
Oh nice, didn't know JS has similar tools. I guess it only makes sense though.
@0xtz_
@0xtz_ 2 жыл бұрын
@@Carberra yes it's amazing u should try it
@Carberra
@Carberra 2 жыл бұрын
If I ever have to use JS I will 😆 Though actually for my work I will have to at some point.
@0xtz_
@0xtz_ 2 жыл бұрын
@@Carberra I started working as A junior Frontend developer I use just Js and Ts all day 😂. And we study React in the UN
@warrior100girl
@warrior100girl 6 ай бұрын
"source.organizeImports": true gives error, saying it expects string
@Carberra
@Carberra 6 ай бұрын
Change it to: "explicit".
@alex-desroches
@alex-desroches Жыл бұрын
thanks
@saviodev777
@saviodev777 8 ай бұрын
thanks
@sofiaislas6239
@sofiaislas6239 10 күн бұрын
Thanks
Functools is one of the MOST USEFUL Python modules
13:37
Carberra
Рет қаралды 36 М.
Powerful VSCode Tips And Tricks For Python Development And Design
15:50
UFC 310 : Рахмонов VS Мачадо Гэрри
05:00
Setanta Sports UFC
Рет қаралды 1,2 МЛН
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 120 МЛН
How Strong Is Tape?
00:24
Stokes Twins
Рет қаралды 96 МЛН
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,8 МЛН
12 VS Code Extensions to INCREASE Productivity 2024
27:13
Devression
Рет қаралды 885 М.
How to use Prettier in VS Code - Code Formatting
8:18
Coding With Adam
Рет қаралды 337 М.
Getting Started with Python in VS Code (Official Video)
10:20
Visual Studio Code
Рет қаралды 253 М.
Python Tutorial for Beginners with VS Code 🐍
13:56
Dave Gray
Рет қаралды 377 М.
Python 101: Learn the 5 Must-Know Concepts
20:00
Tech With Tim
Рет қаралды 1,3 МЛН
Code Formatting In Python With Black - Python Tutorial
7:04
Patrick Loeber
Рет қаралды 56 М.
How to Set up VS Code for Data Science & AI
22:53
Dave Ebbelaar
Рет қаралды 342 М.