在文件中用显式导入自动替换“import*”导入的工具

removestar的Python项目详细描述


重新安装

Build Status

自动用显式导入替换import *导入的工具

需要松饼。

当前限制:

  • 假设星形导入只使用当前文件中的名称(例如 无法替换__init__.py中的星型导入。

对于同一模块内的文件,removestar确定缺少的导入名称 静态的。对于外部库导入,包括标准的导入 库模块,它动态导入模块以确定名称。 这可以用--no-dynamic-importing标志禁用。

请参阅issue tracker。拉动 欢迎提出要求。

安装

pip install removestar

或者如果您使用conda

conda install -c conda-forge removestar

用法

$ removestar file.py # Shows diff but does not edit file.py

$ removestar -i file.py # Edits file.py in-place

$ removestar -i module/ # Modifies every Python file in module/ recursively

为什么import *如此糟糕?

在python中,通常不赞成执行from module import *。它是 在python提示下交互工作时,或在 __init__.py文件(removestar默认跳过__init__.py文件)。

import *不好的一些原因:

  • 它隐藏了实际导入的名称。
  • 这对人类读者和静态分析人员来说都是困难的,比如 pyflakes用于在使用import *时告诉给定名称来自何处。为了 例如,pyflakes在 存在import *
  • 如果有多个import *语句,可能不清楚哪些名称 来自哪个模块。在某些情况下,两个模块可能都有一个给定的名称, 但只有第二次进口才会被使用。这可以打破人们的 python文件中的导入顺序通常不是 很重要。
  • import *通常导入的名称比预期的要多。除非模块 在模块中导入定义了__all__del未使用的名称 级别,import *将导入每个public(不是以 下划线)模块文件中定义的名称。这通常包括 类似于在顶级定义的标准库导入或循环变量 文件。对于来自模块(从__init__.py)的导入,from module import *将包含该模块中定义的每个子模块。在中使用__all__ 模块和__init__.py文件也是很好的实践,因为它们是 即使是在交互使用中,也经常会混淆import * 可以接受。
  • 在python 3中,import *在语法上不允许出现在函数内部。

下面是一些正式的python引用,声明在 文件:

  • The official Python FAQ

    In general, don’t use from modulename import *. Doing so clutters the importer’s namespace, and makes it much harder for linters to detect undefined names.

  • PEP 8(官员 python风格指南:

    Wildcard imports (from <module> import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools.

不幸的是,如果在野外遇到一个使用import *的文件,它 可能很难修复,因为您需要在文件中找到 从*导入。Removestar通过查找名称来简化此过程 从*导入并自动替换文件中的导入行。

示例

假设您有一个类似于

的模块mymod
mymod/
  | __init__.py
  | a.py
  | b.py

使用

# mymod/a.pyfrom.bimport*deffunc(x):returnx+y
# mymod/b.pyx=1y=2

然后removestar的工作方式如下:

$ removestar mymod/

--- original/mymod/a.py
+++ fixed/mymod/a.py
@@ -1,5 +1,5 @@
 # mymod/a.py
-from .b import *
+from .b import y

 def func(x):
     return x + y

默认情况下,这不会编辑a.py。标记-i使其在适当位置编辑a.py

$ removestar -i mymod/
$ cat mymod/a.py
# mymod/a.py
from .b import y

def func(x):
    return x + y

命令行选项

$ removestar --help
usage: removestar [-h] [-i] [--version] [--no-skip-init]
                  [--no-dynamic-importing] [-v] [-q]
                  [--max-line-length MAX_LINE_LENGTH]
                  paths [paths ...]

Tool to automatically replace "import *" imports with explicit imports

Requires pyflakes.

Usage:

$ removestar file.py # Shows diff but does not edit file.py

$ removestar -i file.py # Edits file.py in-place

$ removestar -i module/ # Modifies every Python file in module/ recursively

positional arguments:
  paths                 Files or directories to fix

optional arguments:
  -h, --help            show this help message and exit
  -i, --in-place        Edit the files in-place. (default: False)
  --version             Show removestar version number and exit.
  --no-skip-init        Don't skip __init__.py files (they are skipped by
                        default) (default: True)
  --no-dynamic-importing
                        Don't dynamically import modules to determine the list
                        of names. This is required for star imports from
                        external modules and modules in the standard library.
                        (default: True)
  -v, --verbose         Print information about every imported name that is
                        replaced. (default: False)
  -q, --quiet           Don't print any warning messages. (default: False)
  --max-line-length MAX_LINE_LENGTH
                        The maximum line length for replaced imports before
                        they are wrapped. Set to 0 to disable line wrapping.
                        (default: 100)

更改日志

请参阅CHANGELOG文件。

许可证

MIT

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java无法在未设置数据源的情况下启动springboot应用程序   返回/泛型的类型?   java通过在navigationView中按id重新加载navigationView内容   java实现安卓的状态更新   java Equals()对于两个相等的字符串不返回true   java如何保存屏幕截图(matlab)   java GWT如何在重新加载页面之前确保函数已完全执行   java在Groovy中实现ObjectJSON映射的标准方法是什么?   java在ApacheTomcat中,是否可以通过连接器过滤多个访问日志文件?   java当JVM达到其Xmx限制时,它会强制垃圾收集吗?   如何在JAVA中生成包含特定数字的不同随机数列表?   rcp中透视图之间的java切换   java理解名为“分区”的Linkedlist算法中的无限循环   RestTemplate的java测微计统计信息   Android中使用自定义服务BLE的java读/写特性   java验证输入以确保负数   关于Java扫描器的io基本查询   java如何使用子字符串或其他函数将字符串拆分为单词?   java Storm群集重复元组