python代码格式化工具_autopep8的安装与使用
一、安装
pip install autopep8
1 2 3 4 5 6 7 8 9 10 11 12 | (CloudStorage) D:\learn\IOT\CloudStorage\unit>pip install autopep8 Collecting autopep8 Downloading https: / / files.pythonhosted.org / packages / 5e / 41 / 5bfb10d1a480556ff114cce2ba124416b86197ef7bd6e600b021477856d9 / autopep8 - 1.5 . 6 - py2.py3 - none - any .whl ( 44kB ) 100 % |████████████████████████████████| 51kB 402kB / s Requirement already satisfied: toml in d:\python3. 7.2 \lib\site - packages ( from autopep8) ( 0.10 . 1 ) Collecting pycodestyle> = 2.7 . 0 ( from autopep8) Downloading https: / / files.pythonhosted.org / packages / de / cc / 227251b1471f129bc35e966bb0fceb005969023926d744139642d847b7ae / pycodestyle - 2.7 . 0 - py2.py3 - none - any . whl ( 41kB ) 100 % |████████████████████████████████| 51kB 452kB / s Installing collected packages: pycodestyle, autopep8 Successfully installed autopep8 - 1.5 . 6 pycodestyle - 2.7 . 0 |
二、验证安装是否成功
导入时无报错即可
(CloudStorage) D:\learn\IOT\CloudStorage>python Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> >>> import autopep8 >>>
三、pycharm配置
1、进入settings-tools-external tools添加配置
2、填写配置内容
Name : autopep8
Tool Seetings:
Program : 选择autupep8的下载存储路径
parameters :--in-place --aggressive --aggressive $FilePath$
working directory : $ProjectFileDir$
Advanced Options:
Output filters : $FILE_PATH$\:$LINE$\:$COLUMN$\:.*
3、点击ok,再点击应用
四、验证autopep8格式化工具的效果
before
import math, sys; def example1(): ####This is a long comment. This should be wrapped to fit within 72 characters. some_tuple=( 1,2, 3,'a' ); some_variable={'long':'Long code lines should be wrapped within 79 characters.', 'other':[math.pi, 100,200,300,9876543210,'This is a long string that goes on'], 'more':{'inner':'This whole logical line should be wrapped.',some_tuple:[1, 20,300,40000,500000000,60000000000000000]}} return (some_tuple, some_variable) def example2(): return {'has_key() is deprecated':True}.has_key({'f':2}.has_key('')); class Example3( object ): def __init__ ( self, bar ): #Comments should have a space after the hash. if bar : bar+=1; bar=bar* bar ; return bar else: some_string = """ Indentation in multiline strings should not be touched. Only actual code should be reindented. """ return (sys.path, some_string)
after
import math import sys def example1(): # This is a long comment. This should be wrapped to fit within 72 # characters. some_tuple = (1, 2, 3, 'a') some_variable = { 'long': 'Long code lines should be wrapped within 79 characters.', 'other': [ math.pi, 100, 200, 300, 9876543210, 'This is a long string that goes on'], 'more': { 'inner': 'This whole logical line should be wrapped.', some_tuple: [ 1, 20, 300, 40000, 500000000, 60000000000000000]}} return (some_tuple, some_variable) def example2(): return ('' in {'f': 2}) in {'has_key() is deprecated': True} class Example3(object): def __init__(self, bar): # Comments should have a space after the hash. if bar: bar += 1 bar = bar * bar return bar else: some_string = """ Indentation in multiline strings should not be touched. Only actual code should be reindented. """ return (sys.path, some_string)
使用方法说明:
1、pycharm
选择要格式化的文件---Externer Tools---autopep8
2、命令行
autopep8 --in-place --aggressive --aggressive <filename>
知道、想到、做到、得到
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)