vscode-python: "Format document" doesn't do anything

Environment data

  • VS Code version: version 1.32.3
  • Extension version (available under the Extensions sidebar): 2019.2.5558
  • OS and version: Windows 10, version 1809, OS Build 17763.379
  • Python version (& distribution if applicable, e.g. Anaconda): 3.6.4 64-bit
  • Type of virtual environment used (N/A | venv | virtualenv | conda | …): N/A i think, not 100% sure

Expected behaviour

Format my python document

Actual behaviour

I can’t see it doing anything but running and quitting

Steps to reproduce:

  1. make a python file
  2. write code on it
  3. try to format it 4.??? 5.nothing happens

alt text

Logs

Output for Python in the Output panel

Starting Microsoft Python language server.
##########Linting Output - flake8##########
8,3,E,E303:too many blank lines (2)
8,3,E,E114:indentation is not a multiple of four (comment)
8,3,E,E116:unexpected indentation (comment)
15,3,E,E114:indentation is not a multiple of four (comment)
16,1,W,W293:blank line contains whitespace
17,3,E,E114:indentation is not a multiple of four (comment)
18,1,E,E302:expected 2 blank lines, found 1
29,3,E,E114:indentation is not a multiple of four (comment)
30,3,E,E114:indentation is not a multiple of four (comment)
31,1,E,E305:expected 2 blank lines after class or function definition, found 0
33,13,W,W291:trailing whitespace
33,13,E,E999:SyntaxError: invalid syntax
Microsoft Python Language Server version 0.1.80.0
Initializing for C:\Python36\python.exe

Output from Console under the Developer Tools panel


console.ts:134 [Extension Host] Python Extension: Current value for rule windowsRegistry is {"architecture":3,"path":"C:\\Users\\Desktop-Z\\AppData\\Local\\Programs\\Python\\Python36\\python.exe","version":{"raw":"3.6.5-final","major":3,"minor":6,"patch":5,"prerelease":["final"],"build":[],"version":"3.6.5-final"},"sysPrefix":"C:\\Users\\Desktop-Z\\AppData\\Local\\Programs\\Python\\Python36","fileHash":"01fffab9af52f250db5afb2fb6d879e9dae37dd69f00cd9c256aa83ddacac6aa753009fbdf06448b6bcaba9fe9a2f5e6a29913028d25bef2c4a67003fe031c62","companyDisplayName":"Python Software Foundation","type":"Unknown"}
console.ts:134 [Extension Host] Python Extension: Selected Interpreter from cachedInterpreters, {"architecture":3,"path":"C:\\Users\\Desktop-Z\\AppData\\Local\\Programs\\Python\\Python36\\python.exe","version":{"raw":"3.6.5-final","major":3,"minor":6,"patch":5,"prerelease":["final"],"build":[],"version":"3.6.5-final"},"sysPrefix":"C:\\Users\\Desktop-Z\\AppData\\Local\\Programs\\Python\\Python36","fileHash":"01fffab9af52f250db5afb2fb6d879e9dae37dd69f00cd9c256aa83ddacac6aa753009fbdf06448b6bcaba9fe9a2f5e6a29913028d25bef2c4a67003fe031c62","companyDisplayName":"Python Software Foundation","type":"Unknown"}
console.ts:134 [Extension Host] Python Extension: Selected Interpreter from system, {"architecture":3,"path":"C:\\Users\\Desktop-Z\\AppData\\Local\\Programs\\Python\\Python36\\python.exe","version":{"raw":"3.6.5-final","major":3,"minor":6,"patch":5,"prerelease":["final"],"build":[],"version":"3.6.5-final"},"sysPrefix":"C:\\Users\\Desktop-Z\\AppData\\Local\\Programs\\Python\\Python36","fileHash":"01fffab9af52f250db5afb2fb6d879e9dae37dd69f00cd9c256aa83ddacac6aa753009fbdf06448b6bcaba9fe9a2f5e6a29913028d25bef2c4a67003fe031c62","companyDisplayName":"Python Software Foundation","type":"Unknown","displayName":"Python 3.6.5 64-bit"}
console.ts:134 [Extension Host] Python Extension: Current value for rule workspaceEnvs is nothing
console.ts:134 [Extension Host] Python Extension: setGlobalInterpreter, Class name = g, Arg 1: {"architecture":3,"path":"C:\\Users\\Desktop-Z\\AppData\\Local\\Programs\\Python\\Python36\\python.exe","version":{"raw":"3.6.5-final","major":3,"minor":6,"patch":5,"prerelease":["final"],"build":[],"version":"3.6.5-final"},"sysPrefix":"C:\\Users\\Desktop-Z\\AppData\\Local\\Programs\\Python\\Python36","fileHash":"01fffab9af52f250db5afb2fb6d879e9dae37dd69f00cd9c256aa83ddacac6aa753009fbdf06448b6bcaba9fe9a2f5e6a29913028d25bef2c4a67003fe031c62","companyDisplayName":"Python Software Foundation","type":"Unknown"}, Arg 2: undefined, Return Value: false
console.ts:134 [Extension Host] Python Extension: setGlobalInterpreter, Class name = g, Arg 1: {"architecture":3,"path":"C:\\Users\\Desktop-Z\\AppData\\Local\\Programs\\Python\\Python36\\python.exe","version":{"raw":"3.6.5-final","major":3,"minor":6,"patch":5,"prerelease":["final"],"build":[],"version":"3.6.5-final"},"sysPrefix":"C:\\Users\\Desktop-Z\\AppData\\Local\\Programs\\Python\\Python36","fileHash":"01fffab9af52f250db5afb2fb6d879e9dae37dd69f00cd9c256aa83ddacac6aa753009fbdf06448b6bcaba9fe9a2f5e6a29913028d25bef2c4a67003fe031c62","type":"Unknown"}, Arg 2: undefined, Return Value: false
console.ts:134 [Extension Host] Python Extension: setGlobalInterpreter, Class name = g, Arg 1: {"architecture":3,"path":"C:\\Users\\Desktop-Z\\AppData\\Local\\Programs\\Python\\Python36\\python.exe","version":{"raw":"3.6.5-final","major":3,"minor":6,"patch":5,"prerelease":["final"],"build":[],"version":"3.6.5-final"},"sysPrefix":"C:\\Users\\Desktop-Z\\AppData\\Local\\Programs\\Python\\Python36","fileHash":"01fffab9af52f250db5afb2fb6d879e9dae37dd69f00cd9c256aa83ddacac6aa753009fbdf06448b6bcaba9fe9a2f5e6a29913028d25bef2c4a67003fe031c62","companyDisplayName":"Python Software Foundation","type":"Unknown"}, Arg 2: undefined, Return Value: false
console.ts:134 [Extension Host] Python Extension: Rule = windowsRegistry, result = runNextRule
console.ts:134 [Extension Host] Python Extension: Executing next rule from windowsRegistry
console.ts:134 [Extension Host] Python Extension: Rule = currentPath, result = runNextRule
console.ts:134 [Extension Host] Python Extension: Executing next rule from currentPath
console.ts:134 [Extension Host] Python Extension: Rule = cachedInterpreters, result = runNextRule
console.ts:134 [Extension Host] Python Extension: Executing next rule from cachedInterpreters
3
console.ts:134 [Extension Host] Python Extension: autoSelectInterpreter, Class name = g, Arg 1: undefined, Return Value: undefined
console.ts:134 [Extension Host] Python Extension: Interpreters returned by PipEnvService are of count 0
console.ts:134 [Extension Host] Python Extension: Interpreters returned by WorkspaceVirtualEnvService are of count 0
console.ts:134 [Extension Host] Python Extension: setGlobalInterpreter, Class name = g, Arg 1: {"architecture":3,"path":"C:\\Users\\Desktop-Z\\AppData\\Local\\Programs\\Python\\Python36\\python.exe","version":{"raw":"3.6.5-final","major":3,"minor":6,"patch":5,"prerelease":["final"],"build":[],"version":"3.6.5-final"},"sysPrefix":"C:\\Users\\Desktop-Z\\AppData\\Local\\Programs\\Python\\Python36","fileHash":"01fffab9af52f250db5afb2fb6d879e9dae37dd69f00cd9c256aa83ddacac6aa753009fbdf06448b6bcaba9fe9a2f5e6a29913028d25bef2c4a67003fe031c62","companyDisplayName":"Python Software Foundation","type":"Unknown","displayName":"Python 3.6.5 64-bit"}, Arg 2: undefined, Return Value: false
console.ts:134 [Extension Host] Python Extension: Rule = system, result = runNextRule
console.ts:134 [Extension Host] Python Extension: Executing next rule from system
console.ts:134 [Extension Host] Python Extension: Checking whether locactors have completed locating, Class name = p, , Return Value: true
console.ts:134 [Extension Host] Python Extension: Hide locator refreshing progress, Class name = p, , Return Value: undefined
console.ts:134 [Extension Host] Python Extension: All locators have completed locating, Class name = p, , Return Value: undefined
console.ts:134 [Extension Host] Python Extension: autoSelectInterpreter, Class name = g, Arg 1: undefined, Return Value: undefined
dom.ts:213 [Violation] Added non-passive event listener to a scroll-blocking 'mousewheel' event. Consider marking event handler as 'passive' to make the page more responsive. See https://www.chromestatus.com/feature/5745543795965952
dom.ts:213 [Violation] Added non-passive event listener to a scroll-blocking 'mousewheel' event. Consider marking event handler as 'passive' to make the page more responsive. See https://www.chromestatus.com/feature/5745543795965952
dom.ts:213 [Violation] Added non-passive event listener to a scroll-blocking 'mousewheel' event. Consider marking event handler as 'passive' to make the page more responsive. See https://www.chromestatus.com/feature/5745543795965952
console.ts:134 [Extension Host] Python Extension: Cached data exists getEnvironmentVariables, extension-output-#6
dom.ts:213 [Violation] Added non-passive event listener to a scroll-blocking 'mousewheel' event. Consider marking event handler as 'passive' to make the page more responsive. See https://www.chromestatus.com/feature/5745543795965952
console.ts:134 [Extension Host] Python Extension: Cached data exists getEnvironmentVariables, c:\Users\Desktop-Z\Documents\vscode\Github
8
dom.ts:213 [Violation] Added non-passive event listener to a scroll-blocking 'mousewheel' event. Consider marking event handler as 'passive' to make the page more responsive. See https://www.chromestatus.com/feature/5745543795965952
console.ts:134 [Extension Host] Python Extension: Cached data exists getEnvironmentVariables, c:\Users\Desktop-Z\Documents\vscode\Github\V3n0M-Scanner\v3n0m\v3n0m2.py
dom.ts:213 [Violation] Added non-passive event listener to a scroll-blocking 'mousewheel' event. Consider marking event handler as 'passive' to make the page more responsive. See https://www.chromestatus.com/feature/5745543795965952
console.ts:134 [Extension Host] Python Extension: Cached data exists getEnvironmentVariables, c:\Users\Desktop-Z\Documents\vscode\Github\V3n0M-Scanner\v3n0m\v3n0m2.py


About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 7
  • Comments: 16

Most upvoted comments

I bumped into this problem again.

This time it was not caused by problems in the file parsin, and instead, it was caused incorrect usage of “python.formatting.blackArgs” in the settings.json file. The correct way of having “python.formatting.blackArgs” in settings.json is as follows:

"python.formatting.blackArgs": ["--arg1", "arg1input", "--arg2"] for example: "python.formatting.blackArgs": ["--line-length", "100", "--fast"]

I thought to add this here, for people to find when they face problems with black formatting on vscode.

I have a related issue, if i run yapf from the console it goes ok but if the first line of the python file to format is empty it raises:

TypeError: Cannot read property ‘length’ of undefined

python v3.7.3 VSCode v1.43.1 YAPF v0.29.0 Empty 1st line Screenshot (12) Not empty 1st line Screenshot (14)

Hey,

thanks for getting back at me.

I figured out the problem, which was something i didn’t know, and can be seen from black output from command:

python -m black -v <file name>

The problem was that the python code when parsed, had error’s in it, and so, black couldn’t format it. I hope this helps anyone in future who faces the same problem!

Running the above command gives you output of formatters, i hope this helps.

I had the same problem. is there no way of reporting these errors from within vscode? (is it a generic problem?)

I want to thank @The-da-vinci for helping me get black working again in VSCode.

Symptoms:

  1. Running black my_code.py results in reformatted code as expected

  2. Running black from VSCode fails to reformat the Python code though it does display Formatting with Black in the status bar.

Issue: Corrupted python.formatting.blackArgs

"python.formatting.blackArgs": [
    "\"--line-length\", \"100\""
],

Fix: Remove garbage characters

"python.formatting.blackArgs": [
    "--line-length",
    "100"
],

Now black is running as expected in VSCode!

I am using Yapf on python code and I also cannot format using the VS code command or the right-click option. Below is a screen of my VS Code settings (which are the same at the user, workspace and folder level). Note that if I run from terminal in VS Code the command yapf -i --style Google script.py I do get the file modified, so I don’t think it’s a formatter issue in my case Immagine 2020-12-13 151755