DEV Community

John Nyingi
John Nyingi

Posted on • Updated on

Setting Up PEP8 and Pylint on VS Code

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

Enter fullscreen mode Exit fullscreen mode

Then install PEP8

$ pip install pep8
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

$ code .
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (16)

Collapse
 
dcsan profile image
dc • Edited

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"
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
tgmolinari12 profile image
MT

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".

Collapse
 
ombratteng profile image
Ole-Martin Bratteng

pep8 has been renamed to pycodestyle, so the python.linting.pep8Enabled is not a valid setting anymore. Now it is python.linting.pycodestyleEnabled

Collapse
 
thenextceo profile image
the-next-ceo

well where is the option to open these JSON settings as in ur screenshot

Collapse
 
ianonjuguna profile image
Iano Njuguna

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

In Windows you activate a virtual environment by running the following command:

# Powershell
$ .\env\Scripts\Activate.ps1
Enter fullscreen mode Exit fullscreen mode

or:

# Bash
$ .\env\Scripts\activate
Enter fullscreen mode Exit fullscreen mode
Collapse
 
klvenky profile image
Venkatesh KL

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. :)

Collapse
 
j0nimost profile image
John Nyingi

Sure, I'll have it up pretty soon

Collapse
 
sobolevn profile image
Nikita Sobolev

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!

Collapse
 
mromdhane profile image
Maroua Romdhane

Thank you!

Collapse
 
parsley72 profile image
parsley72

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'?

Collapse
 
moijes12 profile image
Alex

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.

Collapse
 
petermortensen profile image
Peter Mortensen • Edited

This article has been wholesale plagiarised at Medium by Hemprasad Badgujar (incl. all the different misspellings of Visual Studio Code):

medium.com/seminal/setting-up-pep8...

Collapse
 
j0nimost profile image
John Nyingi

thanks for pointing this out

Collapse
 
khwilo profile image
Khwilo Kabaka

Clear and concise article John.

Collapse
 
acceptableengineering profile image
Mark Mutti

Useful guide, thank you!

Collapse
 
j0nimost profile image
John Nyingi

I appreciate your feedback