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

КОМЕНТАРІ • 61

  • @xiaoer-nq3pk
    @xiaoer-nq3pk 9 місяців тому +5

    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 7 місяців тому +28

    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 3 місяці тому

      I believe there is one called Bython!

  • @juan.o.p.
    @juan.o.p. Рік тому +3

    This is extremely useful! Thanks!!!!

  • @FelipeTaranto
    @FelipeTaranto 5 місяців тому

    Thanks!! Thats what I was really looking for

  • @FarazArastu
    @FarazArastu Місяць тому

    game changer - this will save so much time

  • @kajairokajairo2271
    @kajairokajairo2271 Рік тому +1

    Very Interesting, happy to discover this

    • @Carberra
      @Carberra  Рік тому +1

      Glad you enjoyed, and thanks for becoming a member!!

  • @nimamasoumi646
    @nimamasoumi646 2 місяці тому

    thank u so much mate, really helped me out

  • @alantchunay
    @alantchunay 5 місяців тому

    thanks man! you saved my life.

  • @cindinishimoto9528
    @cindinishimoto9528 Рік тому

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

    • @Carberra
      @Carberra  Рік тому +1

      You're very welcome!

  • @MattDeveloper
    @MattDeveloper 5 місяців тому

    Thanks for sharing this!

  • @johnaweiss
    @johnaweiss 8 місяців тому +2

    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.

  • @picchiscam7288
    @picchiscam7288 Рік тому +7

    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 8 місяців тому +1

      he don't reply to issues.. only wants to hear whats works 😅😅 unsubscribed

  • @johnaweiss
    @johnaweiss 8 місяців тому

    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.

  • @wehbe4099
    @wehbe4099 Рік тому +1

    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
    }

  • @PositiveMentalAttitude284
    @PositiveMentalAttitude284 4 місяці тому

    thank you its really helpful

  • @binhna
    @binhna Рік тому

    This is useful, thank you

  • @moeinzand4800
    @moeinzand4800 9 місяців тому

    Thanks man it helped me

  • @rafafazzi7316
    @rafafazzi7316 Рік тому +1

    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.

  • @hasbealam
    @hasbealam 8 місяців тому

    Thank you!

  • @marsdwarf
    @marsdwarf Рік тому

    Thanks so much!

  • @tridibbiswas3361
    @tridibbiswas3361 Рік тому +1

    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! ua-cam.com/video/A6S2nZAXgT8/v-deo.html

  • @Acksakal
    @Acksakal 8 місяців тому

    GOD BLESS YOU!

  • @Face_the_Reality
    @Face_the_Reality Рік тому +1

    Thanks

  • @kuntaldey4471
    @kuntaldey4471 7 місяців тому +1

    "source.organizeImports": true
    true is underlined in yellow
    problem: Incorrect type. Expected "string".
    what to do bro?

    • @kingboy-vh1kd
      @kingboy-vh1kd 3 місяці тому

      Same, did you find any solution?

  • @GamesindieSR
    @GamesindieSR Рік тому

    Thanks keep up and upload more please

  • @orsimhon133
    @orsimhon133 6 місяців тому

    Liked the theme, which is it? :)

    • @Carberra
      @Carberra  6 місяців тому

      Info's in the description 😄

  • @0xtz_
    @0xtz_ Рік тому

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

    • @Carberra
      @Carberra  Рік тому +1

      Oh nice, didn't know JS has similar tools. I guess it only makes sense though.

    • @0xtz_
      @0xtz_ Рік тому

      @@Carberra yes it's amazing u should try it

    • @Carberra
      @Carberra  Рік тому

      If I ever have to use JS I will 😆 Though actually for my work I will have to at some point.

    • @0xtz_
      @0xtz_ Рік тому

      @@Carberra I started working as A junior Frontend developer I use just Js and Ts all day 😂. And we study React in the UN

  • @FricoSimon-li6hd
    @FricoSimon-li6hd 5 місяців тому

    perfect

  • @SniperBoy01
    @SniperBoy01 Рік тому

    super sir
    thanks

  • @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.

  • @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 5 місяців тому

      from the lower right corner chose your environment , play with them it should work

  • @Anarkhea
    @Anarkhea Місяць тому

    Shot in the dark here.. Does Svk say you anything 😂😂

  • @crescentfuze
    @crescentfuze 10 місяців тому

    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  10 місяців тому

      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.

    • @crescentfuze
      @crescentfuze 10 місяців тому

      ​@@Carberra Awesome. Thank you.

  • @johnaweiss
    @johnaweiss 8 місяців тому

    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  8 місяців тому +1

      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.

  • @warrior100girl
    @warrior100girl 2 місяці тому

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

    • @Carberra
      @Carberra  2 місяці тому

      Change it to: "explicit".

  • @saviodev777
    @saviodev777 4 місяці тому

    thanks

  • @alex-desroches
    @alex-desroches 7 місяців тому

    thanks