PyCharm 代码格式化工具:black

black 简介

自动的代码格式化工具,兼容 pep8,项目地址为:

black项目地址

官方给出的简介:

Black is the uncompromising Python code formatter. By using it, you agree to cede control over minutiae of hand-formatting. In return, Black gives you speed, determinism, and freedom from pycodestyle nagging about formatting. You will save time and mental energy for more important matters.

Blackened code looks the same regardless of the project you’re reading. Formatting becomes transparent after a while and you can focus on the content instead.

Black makes code review faster by producing the smallest diffs possible.

安装命令

pip3 install black

使用方法

终端中直接运行

在终端中直接将 black 作为命令来运行,如下截图所示:

终端运行结果

运行成功会有输出提示格式化情况。

PyCharm中配置快捷方式运行

打开路径:

PyCharm→Preferences→Tools→External tool

点击+ 后,输入下面的配置:

配置示例

# Program
$PyInterpreterDirectory$/python3
# Arguments
-m black $FilePath$
# Working directory
$ProjectFileDir$

然后配置快捷键,打开下面的目录

PyCharm→Preferences→keymap→External Tools→black

右键点击 Add Keyboard Shortcut ,如下图设置快捷键:

在这里插入图片描述

成功后,运行快捷方式,会直接格式化你所在窗口的 .py 文件。同样在终端中会显示格式化结果。

PEP8 代码格式

可以参考如下博客:
PEP8简介

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值