在Python中检测非点相对导入的工具?
使用这样的导入被认为是不好的Python:
import my_module
当你进行相对导入时,这会起作用:
from . import my_module
是否有一个工具可以检测我的代码中的这些非点相对导入并警告我,以便我可以将它们更新为点语法? 我的项目有数百个Python模块,我想自动执行此操作。 (可能这样的工具会覆盖 __import__
并检测我运行程序时发生的错误导入。)
有人知道这样的工具吗?
It's considered bad Python to use imports like this:
import my_module
When you are doing a relative import and this would work:
from . import my_module
Is there a tool that can detect these non-dotted relative imports in my code and warn me so I could update them to the dotted syntax? My project has hundreds of Python modules and I would like to do this automatically. (Possibly such a tool would override __import__
and detect the bad imports as they happen when I run the program.)
Does anyone know of such tool?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
[作为答案重新发布,因为它显然做到了这一点]
2to3 会自动转换它们,因为它在 Python 3 中是强制的。
这是 相关源代码(如果您想根据自己的目的修改它)。
或者,您可以仅使用该修复程序运行 2to3:
2to3 -w -f import myproject/
[Reposted as an answer because it apparently did the trick]
2to3 will automatically convert them, because it's compulsory in Python 3.
Here's the relevant source code if you want to modify it for your purposes.
Alternatively, you could just run 2to3 with only that fixer:
2to3 -w -f import myproject/
pylint 给出有关相对导入的警告,以及出于某种原因考虑的大量其他内容,“糟糕的Python”。
pylint gives warnings about relative imports, along with tons of other stuff that's considered, for one reason or another, "bad Python".