loading...

Setting Up PEP8 and Pylint on VS Code

j0nimost profile image John Nyingi Updated on ・2 min read

vscode_and_python

Formatting Python Code to pass the Maintainability test can be hard; especially if you are not receiving some help. It's in human nature to get tired of redundancy, we easily want to get the job done quickly and move on. However, this affects our work output and the quality of our work. When it comes to code quality it's paramount to maintain standards, there's no better way to do so than to follow some already set standards.

PEP8 defines Python coding standards; from variable declaration to formatting of classes. It has it all, this allows you to nicely format your python code. To install the package ensure you are in your project folder and virtualenv is enabled, if not run the following lines in your folder directory

$ virtualenv env

$ source env/bin/activate

Then install PEP8

$ pip install pep8

Now let's checkout Pylint, this tool checks whether we follow PEP8 standards and returns errors where we fail to follow. Furthermore, this tool also does error checking due to syntax errors. To install pylint run the following code;

$ pip install pylint

Since we now have the two needed tools we can now open vs code

$ code .

Once we open our vs code editor; we can select our preferred interpreter, just press
Ctrl + Shift + P.
interpreter
You should see something similar to the above result. Next we select our Python Interpreter
python_env
Just select the python3/2 with virtualenv enabled. This will ensure that Vs code picks up tools we installed in virtual env.

Next we finally activate linting on Vs code.
Follow the following steps
File > Preferences > Settings > Workspace Settings > Python Configuration
Click Edit in settings.json

workspace
Your workspace should match the above linting settings. After editing your json save the settings and start coding. Now everytime you access Vs Code in virtaulenv it will always activate linting.

Posted on by:

j0nimost profile

John Nyingi

@j0nimost

I love learning new things, an explorer and a researcher at heart

Discussion

markdown guide
 

That was simple and straight to the point. Could you write an article to setup vscode for python for an absolute starter. Because, I have setup the venv and I am able to run the python file in pycharm but not in vscode. Any light on that would be great. :)

 

Sure, I'll have it up pretty soon

 

nowadays (june 2020) I get a message of Unknown configuration setting for pep8
has it been renamed flake8 as that does get recognized?

I am trying with various options like this to toggle on/off various linter

{
    "python.linting.pep8Enabled": true,
    "python.linting.pylintEnabled": true,
    "python.linting.flake8Enabled": false,
    "python.linting.enabled": true,
    "python.formatting.provider": "black",
    "python.formatting.blackArgs": [
        "--line-length",
        "120"
    ],    
    "python.linting.flake8Args": [
        "--max-line-length=120",
        "--ignore=E402",
    ],
    "python.linting.pylintArgs": [
        "--max-line-length=180"
    ],
    "python.formatting.autopep8Args": [
        "--max-line-length=180"
    ], 
    "python.pythonPath": "venv/bin/python"
}
 

flake8 is a different linter entirely. If you're still looking for PEP 8 formatting, you can install autopep8 and change your "python.formatting.provider" setting to "autopep8".

 

Maybe you can give wemake-python-styleguide a try? It has even more rules than pylint, but does not even try to mess with types.

It has way less false-positives and is based on flake8.

GitHub logo wemake-services / wemake-python-styleguide

The strictest and most opinionated python linter ever!

wemake-python-styleguide

wemake.services Supporters Build Status Coverage Status Github Action Python Version wemake-python-styleguide


Welcome to the strictest and most opinionated python linter ever.

wemake-python-styleguide logo

wemake-python-styleguide is actually a flake8 plugin with some other plugins as dependencies.

Quickstart

pip install wemake-python-styleguide

You will also need to create a setup.cfg file with the configuration.

We highly recommend to also use:

  • flakehell for easy integration into a legacy codebase
  • nitpick for sharing and validating configuration across multiple projects

Running

flake8 your_module.py

This app is still just good old flake8 And it won't change your existing workflow.

invocation resuts

See "Usage" section in the docs for examples and integrations.

We also support Github Actions as first class-citizens Try it out!

What we are about

The ultimate goal of this project is to make all people write exactly the same python code.

flake8 pylint black mypy wemake-python-styleguide
Formats code?
Finds style issues? 🤔 🤔
Finds bugs? 🤔

Cheers!

 

This is no longer working in VSCode 1.37.1 (2019-08-15). If I follow your instructions then run linting I get a message "Linter pep8 is not installed". Installing again doesn't fix it. Clicking "Select Linter" gives you a list of all supported linters but if you select pep8 it then says "Multiple linters are enabled in settings. Replace with 'pep8'?

 

I think the instructions in here might gelp help. In a nutshell:

  • Open the terminal window
  • Activate the relevant python virtual environment
  • Ensure Pylint is installed within this virtual environment pip install pylint
  • Close all instances of VS Code
  • Launch VS Code from within this terminal window (this will ensure the VS Code process will inherit all of the Virtual Env environment settings)

Let me know if it still doesn't work for you.