Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

autopep8 Python code formatter

📅 2020-Sep-24 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ autopep8 ⬩ 📚 Archive

autopep8 is an automatic Python code formatting tool to make code comply with the PEP8 style guidelines. It is called automatic because it not only checks the code for PEP8, but can go ahead and format it to comply with the guidelines.

autopep8 is built using pycodestyle which is a PEP8 style checker. pycodestyle used to be called pep8 earlier, making it hard for users to distinguish between the guidelines and the tool, so it was later renamed.

$ python3 -m pip install autopep8
$ python3 -m pip uninstall autopep8
$ autopep8 in.py
$ autopep8 in.py > tmp.py
$ mv tmp.py in.py
# First, autopep8 looks for one of these paths in home directory:
~/.config/pep8
~/.config/pycodestyle

# Next, it walks back from PWD looking for one of these filenames:
.pep8
.flake8
setup.cfg
tox.ini
[pycodestyle]
max_line_length = 200

# errors: autopep8 disables some PEP8 rules by default. This enables all.
# E226: By explicitly specifying this, we tell autopep8 to not check it.
ignore = errors,E226

Note that autopep8 does not in fact enforce all PEP8 rules by default. These are the ones marked with * in its error list.

Tried with: autopep8 1.5.4 and Ubuntu 18.04