Introduction
Autopep8是一个将Python代码自动排版为PEP8风格的小工具。
它使用pep8工具来决定代码中的哪部分需要被排版。
Autopep8可以修复大部分pep8工具中报告的排版问题。
Installation
install autopep8 by pip:1
2
3
4
5
6
7
8
9
10$ sudo -H pip install autopep8
[sudo] password for yarving:
Collecting autopep8
Downloading autopep8-1.2.4-py2.py3-none-any.whl (41kB)
100% |████████████████████████████████| 51kB 83kB/s
Collecting pep8>=1.5.7 (from autopep8)
Downloading pep8-1.7.0-py2.py3-none-any.whl (41kB)
100% |████████████████████████████████| 51kB 29kB/s
Installing collected packages: pep8, autopep8
Successfully installed autopep8-1.2.4 pep8-1.7.0
Basic Usage
preparing a python file with bad format
1
2
3
4
5
6
7
8#!/usr/bin/env python
# encoding: utf-8
s1='查看系统'
s2=u'内所有安装'
print s1
print s2
print s1.decode('utf-8')+s2
#print s1 + s2 # this line will raise erroruse pep8 to check this file
1
2
3
4pep8 test.py
test.py:3:3: E225 missing whitespace around operator
test.py:4:3: E225 missing whitespace around operator
test.py:8:1: E265 block comment should start with '# 'use autopep8 to fix the file
Overwrite the original file by commmand:autopep8 --in-place test.py
Then found the file is fixed as below:1
2
3
4
5
6
7
8#!/usr/bin/env python
# encoding: utf-8
s1 = '查看系统'
s2 = u'内所有安装'
print s1
print s2
print s1.decode('utf-8') + s2
# print s1 + s2 # this line will raise error