I am currently working on a python program and am having trouble having VSCode format the python file. As it stands I have a python virtual environment D:/[environment name] and dirrectly within that folder I am storing python files containing code. main.py, test.py the works. When I try to format said files with vscode be it by using the Format Document with comand or by using the auto format feature. Here is my settings.json located within the venv. I am using Python 3.7 because I am working with a library that hasn't been updated to any other version.
{
"[python]": {
"editor.formatOnType": true,
"editor.wordBasedSuggestions": true,
"editor.defaultFormatter": "ms-python.autopep8",
},
"python.formatting.provider": "autopep8",
"python.analysis.typeCheckingMode": "off",
"python.linting.pycodestyleEnabled": true,
"python.linting.enabled": true
}
Sample code from one of the files
from tkinter import *
from tkinter import ttk
root = Tk()
root.mainloop()
x = (1,2)
Output log from Autopep8
2023-05-05 23:48:47.713 [info] [Warn - 11:48:47 PM] Skipping standard library file: d:\project\main.py
2023-05-05 23:49:03.157 [info] [Warn - 11:49:03 PM] Skipping standard library file: d:\project\main.py
2023-05-05 23:50:19.241 [info] [Warn - 11:50:19 PM] Skipping standard library file: d:\project\src\main.py
I have tried restarting, reinstalling extensions reactivating the venv, installing the modules using pip and moving the file into a folder. I have also created a brand new virtual environment made a new file and it still wouldn't format, giving the same error.
VSCode Version 1.78.0 Python Extension Version v2023.8.0
Edit: The linter I have enabled does show the problems with my sample file and I have the folder trusted in VSCode.
If you're using a virtual env then that's why it's broken. I have the same problem with the Microsoft Black extension. It appears that the vscode lsp_server uses a library method
is_stdlib_filethat incorrectly reports project source code in a virtual env as being part of the standard library.You can prove this by temporarily switching the vscode interpreter for the project to the system python and running the formatter. It will work. Of course this is not an acceptable workaround as you are not using the virtual env any more.