Automatically Creating Readable Python Codes

In order to create code, that can be traceable and readable for everyone comments are essential. But there are also style guides to follow in order to create a consistent environment in your code. In Python language, this is regulated with a style guide known as PEP 8. I recommend you to check it out, it has some interesting insights. But if you clicked the link, you will see that it is actually pretty long. To read all of it and to be able to write code as it is intended in this guideline seems to be too much work.

But autopep8 comes to help! It is an automatic formatter to make your code conform with the PEP8. In order to install;

$ pip install --upgrade autopep8

To fix your code with autopep8;

$ autopep8 --in-place <filename>

This one will only fix the whitespace issues. But in order to fix other type of style issues –aggressive option should be used.

$ autopep8 --in-place --aggressive --aggressive <filename>

Multiple usage leads to more aggressive fix. Further options and more comments on the type of issues can be seen in the project page.