Python File not formating in VSCode due to it being skipped by formatter

1.9k Views Asked by At

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.

3

There are 3 best solutions below

2
Andy Brown On

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_file that 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.

0
Kathryn Hall On

I was having the same issue with the Pylint extension assuming my project modules were standard library modules, and refusing to perform any linting.

I installed pylint in the virtual environment (via pip install pylint with the venv as my cwd), and then uninstalled the pylint extension. This seemed to work as pylint now properly lints the files I have open.

I'm not sure how/if this fix could be applied to your situation, as your issue is regarding the python extension, while mine was with the pylint extension.

However, this is the most relevant post I came across while trying to troubleshoot linting inside a venv. As such, I've decided to leave this answer for anyone else who might also stumble across this page.

0
Dan On

The problem happens when the source code is inside the virtual environment, that is, in the same directory that holds bin/, lib/, and pyvenv.cfg:

my_project
|- bin/
|- lib/
|- my_project.py
|- pyvenv.cfg

so the solution is to create the virtual environment in its own subdirectory with a command like

cd my_project
/path/to/python -m venv my_venv

(substituting your desired python interpreter and venv name) to create a directory tree like

my_project
|- my_venv
|  |- bin/
|  |- lib/
|  |- pyvenv.cfg
|- my_project.py

Even in the subdirectory VSCode can detect the venv just fine for debugging and so on, and autopep8 also will work with no other settings changed.