Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python black formatter for vscode not formatting

I'm trying to use black as a formatter for Python on VS Code on Ubuntu 20.04 but it's not working on auto save.

I've selected black in Python>Formatting:Provider. I'm using prettier as my default formatter for which I added a .prettierignore, disabled, and uninstalled to make sure it wasn't interfering with black. I also added a custom path to ./local/bin/black. It works though when I run it through the terminal. How do I make it work?

{
  editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.formatOnSave": true,
  "python.formatting.provider": "black",
  "python.formatting.blackArgs": [
    "-l 120"
  ],
  "editor.formatOnType": true,
  "python.formatting.blackPath": "./local/bin/black"
}
like image 593
VectorXY Avatar asked Mar 20 '21 01:03

VectorXY


People also ask

How do I use black Python format in VS Code?

Go to settings in your VS-Code typing “Ctrl + ,” or clicking at the gear on the bottom left and selecting “Settings [Ctrl+,]” option. Type “format on save” at the search bar on top of the Settings tab and check the box. Search for “python formatting provider” and select “black”.

How do I enable black formatter in VS Code?

Black is "the uncompromising Python code formatter." It can be configured to automatically format your code whenever you save a file in VSCode. Open your VSCode settings, by going 'Code -> Preferences -> Settings'. Black will now format your code whenever you save a *.

How do I fix the format in VS Code?

The code formatting is available in Visual Studio Code through the following shortcuts: On Windows Shift + Alt + F. On Mac Shift + Option + F. On Linux Ctrl + Shift + I.

How to format Python code in VSCode?

Install Microsoft's Python extension in VSCode: Open your VSCode settings, by going 'Code -> Preferences -> Settings'. Search for "python formatting provider" and select "black" from the dropdown menu: In the settings, search for "format on save" and enable the "Editor: Format on Save" option: Black will now format your code whenever you save ...

Is the Black formatter supported in Visual Studio Code?

A Visual Studio Code extension with support for the black formatter. The extension ships with black=22.8.0. This extension is supported for all actively supported versions of the python language (i.e., python >= 3.7). The bundled black is only used if there is no installed version of black found in the selected python environment.

Is there a way to format with black in Python?

There is a new extension, currently pre-release, for formatting with black. See v1.67 Release Notes, Python Black formatting. Once installed in Visual Studio Code, "Black Formatter" will be available as a formatter for python files. Please select "Black Formatter" (extension id:ms-python.black-formatter) as the default formatter.

Why is my Python formatter not working in VS Code?

It might be that other extensions are interfering with your Python formatter. If you use VS Code mostly for web development, you most likely also use Prettier for formatting. I'm talking about Prettier as an extension, not as a package. Check if you have the following configuration in your settings:


Video Answer


1 Answers

There are only a few settings you need to setup black as a formatter on VS Code. It seems you got most of it right, but I am doubtful about using relative paths for blackPath (VS Code should have shown an error if the path is indeed incorrect).

I suggest switching to absolute paths.

Here are my settings:

// User Settings

"editor.defaultFormatter": null,
"editor.formatOnSave": false,  // enable per language
"[python]": {
    "editor.formatOnSave": true
},
"python.formatting.provider": "black",
"python.formatting.blackPath": "/usr/local/bin/black"

// Workspace Settings

"python.formatting.blackPath": "/absolute/path/to/venv/with/black",
"python.formatting.blackArgs": [
    "-l 120"
],

First of all, I suggest getting rid of the editor.defaultFormatter setting (or just set it back to the default null). Instead of setting a default for everything, configure your formatter for each language and for each extension. Here, it's null and then I configure python-specific settings then I have separate ones for other languages (ex. JS and C++). You mentioned something about Prettier, and that could be interfering with VS Code using black.

Second, make sure you are modifying the correct settings. VS Code has 3 sets of settings: User, Workspace, and Folder. I normally have the formatOnSave enabled for Python on the User settings, and provider set to black (using system-wide installed black). On a specific workspace, I have a virtual environment and I override the blackPath to the black specifically installed on that virtual environment. You can also just put all the settings in the User settings or use the same system-wide-installed black. But the main point here is to use absolute paths for both (basically copying the output of which black from the console).

Note that, if you specified blackPath to point to a particular virtual environment, make sure to select that same virtual environment on your workspace.

Lastly, you can check for any issues from the Output tab > Python:

VS Code screenshot of Output

like image 163
Gino Mempin Avatar answered Nov 15 '22 09:11

Gino Mempin