我正在尝试查找每个模块及其子模块所有丢失的导入语句和错误。
有专门的工具来完成我想要做的事情吗?
我写的代码,但看起来真的很糟糕,也许这样的东西已经存在了?:
import os
def find_missing_imports(walk):
for items in walk:
d = items[0]
f_list = items[1]
for f in f_list:
module = f[:-3]
# posix_path
module_path = d.lstrip('.').replace('/','.').lstrip('.')
try:
__import__(module_path, fromlist=[module])
except IndentationError, e:
#print(f,e)
pass
except NameError, e:
print(d,f,e)
pass
except Exception, e:
print(f,e)
pass
walk = [[root,files] for root,dirs,files in os.walk('.') for fn in files if fn.endswith('.py')]
find_missing_imports(walk)
输出:
.[snip]
('./Sky_Group_Inventory_Scanner-wxpython/display_image/Dialogs', 'ImageSelectionFrame.py', NameError("name 'wx' is not defined",))
('./Sky_Group_Inventory_Scanner-wxpython/display_image/Dialogs', 'ItemSpecificsDialog.py', NameError("name 'wx' is not defined",))
('./Sky_Group_Inventory_Scanner-wxpython/display_image/Dialogs', 'ReturnCorrectWatchTitle.py', NameError("name 'wx' is not defined",))
.[snip]
我的项目在重构前是一团糟,但还算有用,现在重构后就坏了。
根据我在 codereview 上的第一篇文章中的建议阅读了“务实的程序员”之后:
我一直在挖掘以下内容的源代码:
/usr/local/lib/python2.7/dist-packages/rope
ROPE 的文档似乎有点稀疏。我也一直在使用 Ninja-IDE,但一直无法找到解决我面临的问题的方法。
总的来说,我认为我错过了重构的意义。
与之前
任何有关填写缺失术语或我什至要问的问题的帮助都会很棒。
pip install pylint
pylint /path/to/module > pylint_output
/path/to/module
是您有兴趣查看的 python 模块的位置。例如:
my_project
|____ moduleA
|____ __init__.py
|____ A.py
|____ moduleB
|____ __init__.py
|____ B.py
./my_project/moduleA
./my_project/moduleB
************* Module module_name.sub_module.class_name.method_name
R: line_no, column: Issue description 'some_name' (issue-type)
C: line_no, column: Issue description 'some_name' (issue-type)
W: line_no, column: Issue description 'some_name' (issue-type)
E: line_no, column: Issue description 'some_name' (issue-type)
F: line_no, column: Issue description 'some_name' (issue-type)
************* Module module_name.sub_module.class_name.method_name
R: line_no, column: Issue description 'some_name' (issue-type)
C: line_no, column: Issue description 'some_name' (issue-type)
W: line_no, column: Issue description 'some_name' (issue-type)
E: line_no, column: Issue description 'some_name' (issue-type)
F: line_no, column: Issue description 'some_name' (issue-type)
pylint -E /path/to/module
将仅返回未定义变量错误。