开发者

Tool to detect non-dotted relative imports in Python?

开发者 https://www.devze.com 2023-01-31 23:27 出处:网络
It\'s considered bad Python to use imports like this: import my_module When you are开发者_开发百科 doing a relative import and this would work:

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?


[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 gives warnings about relative imports, along with tons of other stuff that's considered, for one reason or another, "bad Python".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号