11

I have following Python code:

check_files(original_file = original_file_name, 
                   used_file = used_file_name, 
                   unused_file = unused_file_name)

I want to make it instead to look like:

check_files(original_file = original_file_name, 
            used_file = used_file_name, 
            unused_file = unused_file_name)

Also I want to correct formatting not only for function calls but also that way dictionary key/value pairs and etc.

For Example, in RStudio, if I select the code and press CTRL + I RStudio will correct formating as I have described above. Is there any similar way to correct formating in VSCode?

4
  • what Python formatter are you using? or are you using none? Commented Jun 27, 2023 at 23:24
  • @starball what you mean by formatter? Can you please give me examples? Commented Jun 27, 2023 at 23:38
  • @starball Thank you. For editor.defaultFormatter I have None. Should I select one for Python? It will autoiddent or I need to force it with hotkey? Commented Jun 27, 2023 at 23:44
  • See format code on save. Commented Jun 27, 2023 at 23:51

3 Answers 3

6

Or simply install Black Formatter from VSC extension menu:

https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter

enter image description here

:)

Sign up to request clarification or add additional context in comments.

5 Comments

isn't there any conflict between it and pylance?
@BenyaminJafari I would not know that. I had both installed and didnt notice any conflict....
but as soon as I install pylance, when I press enter (make a break like) within the method's parameters, it indents 8 instead of 4 just for this specific indentation! but without pylance it is always 4.
@BenyaminJafari You may need to adjust the indentation in both? I would try setting for example Black Formatter to 4 and pylance to 0 indents...
I found a check box within the pylance settings representing "Automatically adjust indentation" and disabled it, now that 8 indentations for the function's params is fixed and adjusted to 4.
5

Based on the comments by @starball, @jarmod and additional googling I found that you need to follow those steps:


Step 1. Install Python extension from marketplace: https://marketplace.visualstudio.com/items?itemName=ms-python.python


Step 2. Install one of the formatter packages for Python.

The Python extension supports source code formatting using either autopep8 (the default), black, or yapf.

from and More about it here: https://code.visualstudio.com/docs/python/editing#_formatting


Step 3. Select which code formatter you want to use in python.formatting.provider which is in settings>Extensions>Python (this maybe automatically set after step 1 and step 2). Also, in settings>Extensions>Python there are more options to select.



How to use formatting:

The code formatting is available in Visual Studio Code (VSCode) through the following shortcuts or key combinations:

On Windows Shift + Alt + F

On macOS Shift + Option + F

On Linux Ctrl + Shift + I

Format Selection (Ctrl+K Ctrl+F) - Format the selected text.

Or you can use right click menu:

enter image description here

from: https://mkyong.com/vscode/how-to-format-source-code-in-visual-studio-code-vscode/

and from: https://code.visualstudio.com/docs/editor/codebasics#_formatting

Comments

0

A simple solution is to install extension autopep8 extension. and then add this

"[python]": {
  "editor.defaultFormatter": "ms-python.autopep8"
}

to your settings.json file. click file/preferences/settings/ and click on top icon. it's above the search box in settings and on right side. when you hover on the icon you will see something like "Open Settings (JSON)" then paste the above code inside. you are good to go

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.