PyCharm - Optimizing Imports (优化导入)

1. Introduction

Sooner or later, some of the imported classes or packages become redundant to the code.
迟早,一些导入的 classes or packages 对代码来说是多余的。

Typically, you have to stop what you are doing, scroll to the head of the file, find the unused imports, and remove them. It is rather easy to forget to remove imports when you remove usages.
通常,您必须停止正在执行的操作,滚动到文件的开头,找到未使用的导入,然后将其删除。很容易忘记删除导入。

PyCharm provides the Optimize Imports feature, which enables you, whenever it is convenient, to remove unused imports from your current file, or from all files in the current directory at once. This helps you avoid unused, excessive and duplicating imports in your project.
PyCharm 提供了优化导入功能,该功能使您可以在方便时随时从当前文件或当前目录中的所有文件中删除未使用的导入。这可以帮助您避免项目中未使用,过多和重复的导入。

excessive [ɪk'sesɪv]:adj. 过分的,过度的

One can remove unused import statements in the entire project or in the current file only.
可以删除整个项目或仅当前文件中未使用的导入语句。

Besides cleaning the code from the unused imports, PyCharm formats the existing import statements according to the Style Guide for Python Code. So doing, PyCharm splits import statements into separate lines, and sorts them into groups (refer to the Imports section for details).
除了清除未使用的导入中的代码外,PyCharm 还根据 《Python 代码样式指南》 格式化现有的导入语句。这样,PyCharm 会将 import 语句拆分为单独的行,并将其分类为组 (refer to the Imports section for details)。

scroll [skroʊl]:v. 滚动,滚屏 n. 卷轴,(石刻或木雕的) 涡卷形装饰
alphabetically [ˌælfəˈbetɪkli]:adv. 照字母顺序排列地

在 Pycharm 编辑环境中冗余的 imports 语句都是灰色显示的。

Also, imports are sorted alphabetically within the respective groups:
导入在各个组中按字母顺序排序:

在这里插入图片描述

2. Optimizing imports in project

To optimize imports in the entire project, follow these steps:

  1. Place the caret at the Project tool window and do one of the following:
    • On the main menu, choose Code | Optimize Imports.
    • Press Ctrl+Alt+O.
    • The Optimize Imports dialog box opens.
  2. If your project is under version control, the option Only VCS changed files is enabled. Select or clear this option as required. (如果你的项目受版本控制,则启用 Only VCS changed files 选项。根据需要选择或清除此选项。)
  3. Click Run.

3. Optimizing imports in the current file

To optimize imports in the currently opened file, do one of the following:

  • On the main menu, choose Code | Optimize Imports.
  • Press Ctrl+Alt+O.
  • Place the caret at the import statements and press Alt+Enter, or just click /help/img/idea/2016.3/intentionBulb.png to show the list of suggested intention actions, and then choose Optimize imports.
caret [ˈkærət]:n. 脱字符号,插入符号
dialog ['daɪəlɒɡ]:n. 对话,会话
intention [ɪnˈtenʃn]:n. 意图,目的,意向,愈合

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值