Enforce PEP-8 linting and formatting in VS Code (Linux)

7.1k views Asked by At

I'm under a Linux Mint 20 environment and for Python programming I have been using pyenv, which is the recommended method to install Python under Linux

I usually work with VS Code and just recently had the chance to try PyCharm. I was surprised with its linting and formatting capabilities, makes programming really pleasant. However, while in PyCharm I've got all kinds of useful warnings and code is always properly formatted, in VS Code this happens partially only.

To mention an example, in PyCharm after an import if I only leave one blank line I got this warning:

PEP 8: E302 expected 2 blank lines, found 1

After formatting the file PyCharm automagically adds the two blank lines. The same pep 8 infraction in VS Code is ignored, no warnings, and after formatting it does nothing.

I installed all linters and formatter providers available in my virtual environment for testing purposes with no results. This is one of the many configurations that I have tried:

{
    "python.formatting.provider": "autopep8",
    "python.linting.pylintEnabled": true,
    "python.linting.flake8Enabled": true,
}

How can I possibly replicate the excellent linting and formatting from PyCharm in VS Code?

UPDATE:

For my surprise I replicate this configuration under a Windows 10 environment using pipenv instead of pyenv and I successfully replicated the linting and formatting capabilities from PyCharm in VS Code. I must assume the problem has to do with the OS or pyenv, maybe?

2

There are 2 answers

1
Ujjwal Agrawal On

Hey try installing autopep8 in your env. Then activate the same env as interpreter in your VScode. Select your code with ctrl+a and then do right click mouse you will get the formatting option. Also make sure that you have python and intelisense extension installed from the extension market place of VScode.

0
Riyas On

Additional suggestion, Try pylint with Prettier extension in VSCode, will do decent formatting job, while saving python code changes. It was effective for me.