PyCharm 代码格式化工具:black

您所在的位置:网站首页 pycharm怎么写代码雨 PyCharm 代码格式化工具:black

PyCharm 代码格式化工具:black

2023-08-13 12:31| 来源: 网络整理| 查看: 265

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简介



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3