0

I am trying to set up a formatter for VS Code. I would like to use Black, but I am struggling to get it to work. When I try to use the Format Document command, I get an error that says "Python auto formatting: Extension 'Python Language Basics' cannot format ~'/'".

I have followed online tutorials and done the following:

  • pip3 install black
  • Set black as the python formatting provider (went to settings, found "Python › Formatting: Provider", selected Black from the drop down)
  • Turn on format on save
  • Set default formatter to null

Why is VS Code still defaulting to 'Python Language Basics'? How do I change this to Black or yapf?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
user12457151
  • 853
  • 2
  • 12
  • 25
  • 1
    Have you tried using the [Python extension for VS Code](https://marketplace.visualstudio.com/items?itemName=ms-python.python)? – Brett Cannon Jun 15 '20 at 23:15

2 Answers2

3

If it's still invoking the built-in Python Language Basics extension, then that means Black isn't configured correctly. There seems to be nothing wrong with the steps you did, so I would suggest just double-checking the settings.

  • pip3 install black

Here, you have to make sure that the Python environment where you installed Black is the same Python environment you activate in VS Code. See the docs on selecting and activating an environment. If you've got multiple Python versions or you are using virtual environments, you'll have to make sure you're activating the correct one.

Python: Select Interpreter

enter image description here

(test-py38) gino@~$ pip install black
Requirement already satisfied: black in ./.venvs/test-py38/lib/python3.8/site-packages (19.10b0)
...
(test-py38) gino@~$ which black
/Users/gino/.venvs/test-py38/bin/black

You can set python.formatting.blackPath to explicitly specify the path to Black:

"python.formatting.blackPath": "/Users/gino/.venvs/test-py38/bin/black"

You can also try checking if Black is working by calling it manually.

(test-py38) gino@~$ black test.py
reformatted test.py
All done! ✨  ✨
1 file reformatted.
  • Set black as the python formatting provider (went to settings, found "Python › Formatting: Provider", selected Black from the drop down)
  • Turn on format on save
  • Set default formatter to null

Again, double-check your settings.json

"editor.defaultFormatter": null,
"editor.formatOnSave": true,
"python.formatting.provider": "black",

Also, make sure that you're configuring the correct settings, since VS Code has 3 sets of settings User, Workspace, and Folder settings.

enter image description here

When I try to use the format document command

Given that you enabled formatOnSave, you really don't need to call the Format Document command. When you save the file, it should automatically call the defaultFormatter (which is null) so it calls the language-specific formatter (python.formatting.provider).

If you really want to call that command manually, try Format Document With.. then select Python.

enter image description here

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
0

The error information which you have given out was a little unusual because 'autopep8' was the default formatting, and if you have not installed the formatting provider, the VSCode will prompt you to install it.

The 'Python Language Basics' was a builtin extension, and it says: Extension 'Python Language Basics' cannot format. The function was provided by the python extension. So, maybe you can try to reinstall the related extensions, such as 'Python', reopen VSCode, or even reinstall VSCode. Someone also meets the same problem, and the problem was solved through a reinstallation. here

Steven-MSFT
  • 7,438
  • 1
  • 5
  • 13