How to check coding standards in Python

Introduction

When you start your first Python project, the code that you or your team members are going to write should be consistent with the standards defined so that your code is readable. This will make it easier for anyone who comes after you, to develop the application, to understand the code.

pycodestyle module

We are going to use pycodestyle to check whether our not our code follows the coding standards. Earlier our module was named as pep8, but to avoid confusion, it was renamed to pycodestyle.

PEPstands for Python Enhancement Proposal. is just a simple document that provides the guidelines, best practices, design, and style standards to check your Python code.

This is a very easy module to use. First, install the module by running the following command:

pip install pycodestyle

After this, you can run the command below to check any Python file for standard checking:

pycodestyle <python_file_name>.py

Run the below code to see where this module identified the standard violation. When you click the “Run” button, the command is run internally:

pycodestyle main.py

Below is a simple code that prints the current working directory:

import os, sys
def printDirectory():
print(os.getcwd())
printDirectory()

Explanation:

  • On line 1, we import the packages.
  • On line 3, we define the function that prints the current working directory.
  • On line 6, we call the function.

After running the code, you can see that we have not incorporated all the Python coding standards; so, we should make changes to our code accordingly to make it more readable.

Free Resources