How to know your python code is PEP8 compliant?

Everyday, I am learning new things about this awesome language. One amongst the great things it offers is its readability and how it is ingrained in its syntax. It really makes newbies write readable code. This post is about how to know whether your code is PEP8 compliant. PEP8 is the standard code styling guide followed by python developers worldwide.

  1. Install the command-line program pep8
    $ pip install pep8
  2. Execute it for the python file, you want to check, to get the code violations. E.g.
    $ pep8 test.py
    test.py:7:1: W191 indentation contains tabs
    test.py:9:1: W191 indentation contains tabs
    test.py:10:1: W191 indentation contains tabs
    test.py:14:80: E501 line too long (91 > 79 characters)

That’s it! Its as simple as that (simplicity – another great feature of python). You can run it on all of your files together (or on a series of files) :

  $ pep8 *.py

Disadvantage: It will give a long list of violations if you are new to python. I recommend running it after every python file you write. Its more manageable that way. Once you are experienced enough, then the above technique will be helpful since the number of violations would be lesser.

References
Python Guide: Code Style

Advertisements

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s