How can I check the syntax of Python script without executing it?
我曾经使用
您可以通过编译来检查语法:
1 | python -m py_compile script.py |
您可以使用这些工具:
- 毕克克
- 芘
- 皮林特
1 2 3 4 5 | import sys filename = sys.argv[1] source = open(filename, 'r').read() + ' ' compile(source, filename, 'exec') |
保存为checker.py并运行
可能有用的在线检查程序pep8:http://pep8online.com/
出于某种原因(我是个新手…)-m调用不起作用…
这里有一个bash包装函数…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | # --------------------------------------------------------- # check the python synax for all the *.py files under the # <<product_version_dir/sfw/python # --------------------------------------------------------- doCheckPythonSyntax(){ doLog"DEBUG START doCheckPythonSyntax" test -z"$sleep_interval" || sleep"$sleep_interval" cd $product_version_dir/sfw/python # python3 -m compileall"$product_version_dir/sfw/python" # foreach *.py file ... while read -r f ; do \ py_name_ext=$(basename $f) py_name=${py_name_ext%.*} doLog"python3 -c "import $py_name"" # doLog"python3 -m py_compile $f" python3 -c"import $py_name" # python3 -m py_compile"$f" test $! -ne 0 && sleep 5 done < <(find"$product_version_dir/sfw/python" -type f -name"*.py") doLog"DEBUG STOP doCheckPythonSyntax" } # eof func doCheckPythonSyntax |