Skip to content

Commit a5f34e1

Browse files
committed
Add more troubleshooting to linting and formatting for Python
1 parent 63bfbe0 commit a5f34e1

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

docs/python/formatting.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ Formatting makes source code easier to read by human beings. By enforcing partic
1414

1515
[Linting](/docs/python/linting.md) helps to prevent errors by analyzing code for common syntactical, stylistic, and functional errors and unconventional programming practices. Although there is a little overlap between formatting and linting, the two capabilities are complementary.
1616

17+
18+
## Open a folder or workspace
19+
20+
Formatting for Python files is only supported for open folders or workspaces in VS Code, not single files. You can open a folder or workspace containing Python files in VS Code through **File** > **Open Folder...**.
21+
1722
## Choose a formatter
1823

19-
Install the formatting tool of your choice from the VS Code [Marketplace](https://marketplace.visualstudio.com/vscode).
24+
Search the VS Code [Marketplace](https://marketplace.visualstudio.com/vscode) for the formatter extension of your choice.
2025

2126
Microsoft publishes the following formatting extensions:
2227

@@ -89,15 +94,16 @@ You can also add the following setting to your User `settings.json` file to enab
8994

9095
## General formatting settings
9196

92-
Each formatter extension may have its own settings, but the ones below are supported by both [autopep8](https://marketplace.visualstudio.com/items?itemName=ms-python.autopep8) and [Black Formatter](https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter):
97+
98+
You can refer to each formatter extension's README for more details on the supported settings. The following settings are supported by most formatter extensions:
9399

94100
| Setting Suffix<br/> | Default value | Description |
95101
| --- | --- | --- |
96102
| args | `[]` | Arguments to be passed to the formatter. Each argument should be passed as a separate string in the array. <br> For example:<br> `black-formatter.args: ["--line-length", "100"]` |
97103
| importStrategy | `useBundled` | When set to `useBundled`, the extension uses the version of the tool that it ships with. When set to `fromEnvironment`, it attempts to load from your selected Python environment first, otherwise it falls back to the bundled version. |
98104
| path | `""` | Path to the formatter binary to be used for formatting. **Note:** Using this option may slow down formatting. |
99105
| interpreter | `[]` | When set to a path to a Python executable, the extension will use that to launch the formatter server and its subprocesses. |
100-
| showNotifications | `off`| Controls when notifications are displayed by this extension. Supported values are `off`, `always`, `onError`, and `onWarning`. |
106+
| showNotifications | `off`| Controls when notifications are displayed by the extension. Supported values are `off`, `always`, `onError`, and `onWarning`. |
101107

102108
## Troubleshoot formatting
103109

@@ -106,9 +112,11 @@ If formatting fails, check the following possible causes:
106112
| Problem | Solution |
107113
| --- | --- |
108114
| There are multiple formatters available for Python files. | Set the default formatter by following the instructions in [the section above](#set-a-default-formatter). |
115+
| No "Format Document With..." option is available. | If you don't see this option in the context menu, it's likely you don't have a formatter extension installed or enabled in VS Code. Reference the [Choose a Formatter](#choose-a-formatter) section to see how you can install a Python formatter extension. |
109116
| Custom arguments for the formatter are incorrect. | Check that the appropriate `<formatter>.path` setting does not contain arguments, and that `<formatter>.args` contains a list of individual top-level argument elements. |
117+
| "You have deprecated linting or formatting settings" notification displayed. | If you are seeing this notification, it means you have settings such as `python.linting` or `python.formatting` in VS Code. These settings are no longer supported by the Python extension, as [linting and formatting support has been migrated to tools extensions](https://github.com/microsoft/vscode-python/wiki/Migration-to-Python-Tools-Extensions). | Find where these settings are defimed in VS Code by opening the Command Palette (`kb(workbench.action.showCommands)`) and running the **Preferences: Open User Settings (JSON)** command. If they're not in your User settings, then run the **Preferences: Open Workspac Settings (JSON)** command. Then delete the deprecated settings. <br> **Note**: If you're using any of the extension in the [Remote Development extension pack](/docs/remote/remote-overview.md#remote-development-extension-pack), you can also check the remote settings by running the **Preferences: Open Remote Settings (JSON)** command. |
110118
| The **Format Selection** command fails when using Black Formatter. | `black` does not support formatting sections of code. To work around this limitation, you can disable format on paste and set `formatOnSave` to format the whole file with the following settings: `"[python]": {"editor.formatOnPaste": false, "editor.formatOnSaveMode": "file"}`.|
111-
| The document isn't formatted. | Check the formatter extension's Output channel to understand why the formatter has failed (run the **Output: Focus on Output** command in the Command Palette and then select the formatter extension channel).|
119+
| Formatting doesn't work even though I have a formatter extension installed. | Formatting can fail for various reasons: there may be syntax issues in your code, an unsupported version of Python is being used, there's no folder open in VS Code, etc. Check the formatter extension's Output channel to understand why the formatter has failed (run the **Output: Focus on Output** command in the Command Palette and then select the formatter extension channel).|
112120

113121
> **Note**: If you don't find your preferred formatter listed above, you can add support via an extension. The [Python Extension Template](/api/advanced-topics/python-extension-template.md) makes it easy to integrate new Python tools into VS Code.
114122

docs/python/linting.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ MetaSocialImage: images/tutorial/social.png
1111

1212
# Linting Python in Visual Studio Code
1313

14-
Linting highlights syntactical and stylistic problems in your Python source code, which often helps you identify and correct subtle programming errors or unconventional coding practices that can lead to errors. For example, linting detects use of an uninitialized or undefined variable, calls to undefined functions, missing parentheses, and even more subtle issues such as attempting to redefine built-in types or functions. Linting is distinct from [Formatting](/docs/python/formatting.md) because linting analyzes how the code runs and detects errors whereas formatting only restructures how code **appears**.
14+
Linting highlights syntactical and stylistic problems in your Python source code, which often helps you identify and correct subtle programming errors or unconventional coding practices that can lead to errors. For example, linting detects use of an uninitialized or undefined variable, calls to undefined functions, missing parentheses, and even more subtle issues such as attempting to redefine built-in types or functions. Linting is distinct from [Formatting](/docs/python/formatting.md) because linting analyzes how the code runs and detects errors whereas formatting only restructures how code appears.
1515

16-
> **Note**: Stylistic and syntactical code detection is enabled by the Language Server. To enable third-party linters for additional problem detection, you can enable them by using the **Python: Select Linter** command and selecting the appropriate linter.
16+
> **Note**: Syntactical code detection is enabled by default in the Python extension's Language Server. To learn how you can configure the Language Server, see [Language Server Settings](/docs/python/settings-reference.md#python-language-server-settings). This document covers how you can enable linting for additional code detection including stylistic checks.
1717
18-
## Choose a linter
18+
## Open a folder or workspace
19+
20+
Linting for Python files is only supported for open folders or workspaces in VS Code, not single files. You can open a folder or workspace containing Python files in VS Code through **File** > **Open Folder...**.
1921

20-
Install the linting tool of your choice from the VS Code [Marketplace](https://marketplace.visualstudio.com/vscode).
22+
## Choose a linter
2123

22-
Microsoft publishes the following linting extensions:
24+
Search the VS Code [Marketplace](https://marketplace.visualstudio.com/vscode) for the linter extension of your choice.
2325

26+
Microsoft publishes the following linting extensions for Python:
2427
| Linter | Extension |
2528
| ------ | ------------------------------------------------------------------------------- |
2629
| Pylint | [https://marketplace.visualstudio.com/items?itemName=ms-python.pylint](https://marketplace.visualstudio.com/items?itemName=ms-python.pylint) |
@@ -38,10 +41,15 @@ Linting extensions offered by the community:
3841
3942
## General Settings
4043

44+
You can refer to each linter extension's README for more details on the supported settings. The following settings are supported by most linter extensions:
45+
4146
| Setting | Default | Description |
4247
| -------------- | ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
4348
| args | `[]` | Arguments to be passed to the linter. **Note**: The officially supported linters run on individual open files. Make sure your configuration applies in that scenario. |
4449
| importStrategy | `useBundled` | When set to `useBundled`, the extension uses the version of the tool that it ships with. When set to `fromEnvironment`, it attempts to load from your selected Python environment first, otherwise it falls back to the bundled version. |
50+
| path | `""` | Path to the linter binary to be used for linting. **Note:** Using this option may slow down formatting. |
51+
| interpreter | `[]` | When set to a path to a Python executable, the extension will use that to launch the linting server and its subprocesses. |
52+
| showNotifications | `off`| Controls when notifications are displayed by the extension. Supported values are `off`, `always`, `onError`, and `onWarning`. |
4553

4654
## Disable linting
4755

@@ -73,7 +81,9 @@ Linters report issues with some predefined severity. This can be changed using `
7381

7482
| Issue | Cause | Solution |
7583
| --- | --- | --- |
76-
| No problems reported | No Python has been selected for your workspace. | Look at the logs for the linter you are using and check the path to the Python environment it's using. If there is no Python selected, run the **Python: Select Interpreter** command from the Command Palette and select an existing interpreter for your workspace. |
84+
| Linter extension is not reporting any problems. | No Python has been selected for your workspace. | Look at the logs for the linter you are using and check the path to the Python environment it's using. If there is no Python selected, run the **Python: Select Interpreter** command from the Command Palette and select an existing interpreter for your workspace. |
85+
| "You have deprecated linting or formatting settings" notification displayed | If you are seeing this notification, it means you have settings such as `python.linting` or `python.formatting` in VS Code. These settings are no longer supported by the Python extension, as [linting and formatting support has been migrated to tools extensions](https://github.com/microsoft/vscode-python/wiki/Migration-to-Python-Tools-Extensions). | Find where these settings are defimed in VS Code by opening the Command Palette (`kb(workbench.action.showCommands)`) and running the **Preferences: Open User Settings (JSON)** command. If they're not in your User settings, then run the **Preferences: Open Workspac Settings (JSON)** command. Then delete the deprecated settings. <br> **Note**: If you're using any of the extension in the [Remote Development extension pack](/docs/remote/remote-overview.md#remote-development-extension-pack), you can also check the remote settings by running the **Preferences: Open Remote Settings (JSON)** command. |
86+
| Linting doesn't work even though I have a linter extension installed. | Linting can fail for various reasons, such as an unsupported version of Python is being used, there's no workspace open in VS Code, etc. Check the linter extension's Output channel to understand why the linter has failed (run the **Output: Focus on Output** command in the Command Palette and then select the linter extension channel).|
7787

7888
## Next steps
7989

0 commit comments

Comments
 (0)