python 如何检查python中的编译错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2117586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:50:48  来源:igfitidea点击:

How can I check compilation errors in python?

pythoncompilation

提问by stacka

#!/usr/bin/python  
str = "this"  
if(1):  
  print "Hi"  
else:  
  print str.any_random_function()  

This doesn't fail when I run the program. I tried py_compile but that didn't indicate the error in the 'else' loop either. Now how can I compile the program and detect errors reliably in python code?

当我运行程序时,这不会失败。我试过 py_compile 但这也没有表明'else'循环中的错误。现在如何编译程序并可靠地检测 python 代码中的错误?

Thanks.

谢谢。

回答by Geo

I think your best bet would be pylint.

我认为你最好的选择是pylint

回答by AndiDog

Python is a dynamic language, so you can't simply check for compiling errors like in static languages (C/C++/Java). If you assign str.any_random_function, the above code would be correct (okay that's a bad example...).

Python 是一种动态语言,因此您不能像静态语言 (C/C++/Java) 那样简单地检查编译错误。如果你赋值str.any_random_function,上面的代码是正确的(好吧,这是一个不好的例子......)。

I'd suggest you to use PyDev for Eclipsewhich automatically finds many common problems in your code, like missing functions/modules etc. It also supports pylint(optional).

我建议您将PyDev 用于 Eclipse,它会自动查找代码中的许多常见问题,例如缺少函数/模块等。它还支持pylint(可选)。