是否有 IDE/实用程序来重构 Python * 导入以使用标准 module.member 语法? [英] Is there an IDE/utility to refactor Python * imports to use standard module.member syntax?

查看:31
本文介绍了是否有 IDE/实用程序来重构 Python * 导入以使用标准 module.member 语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近的任务是维护大量使用 from module import * 的代码.

I was recently tasked with maintaining a bunch of code that uses from module import * fairly heavily.

这个代码库已经变得足够大,以至于导入冲突/命名模糊/这个函数到底是从哪里来的,大约有八个导入的模块有一个同名?!"主义变得越来越普遍.

This codebase has gotten big enough that import conflicts/naming ambiguity/"where the heck did this function come from, there are like eight imported modules that have one with the same name?!"ism have become more and more common.

展望未来,我一直在使用显式成员(即 import module ... module.object.function() 以使我所做的维护工作更具可读性.

Moving forward, I've been using explicit members (i.e. import module ... module.object.function() to make the maintenance work I do more readable.

但我想知道:是否有一个 IDE 或实用程序可以稳健地解析 Python 代码并将 * import 语句重构为模块 import 语句,然后将完整的模块路径添加到对该模块成员的所有引用?

我们没有大量使用元编程/反射/inspect/monkeypatching,所以如果上述 IDE/util 在这些事情上表现不佳,那没关系.

We're not using metaprogramming/reflection/inspect/monkeypatching heavily, so if aforementened IDE/util behaves poorly with such things, that is OK.

推荐答案

不是一个完美的解决方案,但我通常会这样做:

Not a perfect solution, but what I usually do is this:

  1. 打开 Pydev
  2. 删除所有 * 导入
  3. 使用 optimize imports 命令 (ctrl+shift+o) 重新添加所有导入
  1. Open Pydev
  2. Remove all * imports
  3. Use the optimize imports command (ctrl+shift+o) to re-add all the imports

大致解决了问题:)

如果您想自己构建解决方案,请尝试 http://docs.python.org/库/modulefinder.html

If you want to build a solution yourself, try http://docs.python.org/library/modulefinder.html

这篇关于是否有 IDE/实用程序来重构 Python * 导入以使用标准 module.member 语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆