如何在不执行Python脚本的情况下检查其语法?

469

我曾经使用 perl -c programfile 来检查 Perl 程序的语法,并在不执行它的情况下退出。是否有类似的方法可以用来检查 Python 脚本的语法?

9个回答

733

你可以通过编译来检查语法:

python -m py_compile script.py

12
"import script",但所有代码必须在函数中。这是一个好的编程实践。我甚至将其应用于Shell脚本。从这里开始,距离单元测试只有一小步了。 - Henk Langeveld
75
python -m compileall 命令可以递归地编译整个目录,并且它还具有更好的命令行界面。 - C2H5OH
9
不错的回答,但我该如何防止它创建“.pyc”文件?另外,“.pyc”文件有什么用途? - pdubois
6
对于Python 2.7.9版本,当存在“-m py_compile”时,我发现无论是“-B”还是“PYTHONDONTWRITEBYTECODE”,都不能抑制**.pyc**文件的创建。 - DavidRR
2
我运行了脚本 python -m py_compile src/nike_run.py,它没有出现错误信息,但是在运行时代码会崩溃,并提示消息“TypeError: run_test_in_batch() missing 1 required positional argument: 'total_test_rownum'”,似乎它无法检测到这种错误。如果有误,请纠正我。 - Ninja
显示剩余12条评论

72

20
所有这些工具都不仅限于检查语法。但这并不是答案。 - Matt Joiner
41
所有这些都检查语法,因此答案是正确的。其他检查是一个非常有用的奖励。 - johndodo
2
PyChecker自2011年以来就没有更新了,也不支持Python 3。 - JPaget
1
Pylint非常可定制。我曾经在我的代码中添加了许多禁用指令来使用Pylint,但最近我只是设置了一个pylintrc来关闭所有我不关心的警告。有了正确的pylintrc,这可能是最好的答案。 - user1277476
Pyflakes非常完美。 - cactus

28
import sys
filename = sys.argv[1]
source = open(filename, 'r').read() + '\n'
compile(source, filename, 'exec')

将此内容保存为checker.py并运行python checker.py yourpyfile.py


1
对于一个小脚本集合来说,Makefile 有点过于繁重,但它能够完成工作并且不会产生任何不必要的文件。 - proski
1
这是一个老的答案,但需要注意的是它只检查语法,而不检查脚本是否能够成功执行。 - vallentin
2
非常感谢。它有效了。只有一个评论,如果代码正确,则没有答案。否则会显示带有行号的错误消息。 - musbach

27

这里是另一种解决方案,使用 ast 模块:

python -c "import ast; ast.parse(open('programfile').read())"

要在Python脚本中干净地执行此操作:

import ast, traceback

filename = 'programfile'
with open(filename) as f:
    source = f.read()
valid = True
try:
    ast.parse(source)
except SyntaxError:
    valid = False
    traceback.print_exc()  # Remove to silence any errros
print(valid)

3
非常棒的一行代码,不需要导入所有库或生成 .pyc 文件。谢谢! - mmell
1
应该接受这个答案。根据所接受的答案编译这些文件是过度的,当一个人只想知道语法是否有效时。 - Nils Lindemann
1
请注意,ast.parse(string)等同于compile(string, filename='<unknown>', mode='exec', flags=ast.PyCF_ONLY_AST)参考链接 - Nils Lindemann
1
正是我想要做的事情。 - Tanveer Badar
这也不执行顶层代码。Python在编译或导入模块时实际上会执行模块;只是“执行def语句”是Python创建函数对象的方式,然后这些对象成为模块对象的属性。 - Karl Knechtel

21

Pyflakes执行你的要求,它只检查语法。根据文档:

Pyflakes有一个简单的承诺:它永远不会抱怨风格,并且将尝试非常非常努力地永远不发出错误警报。

Pyflakes比Pylint或Pychecker更快。这主要是因为Pyflakes仅独立地检查每个文件的语法树。

安装和使用:

$ pip install pyflakes
$ pyflakes yourPyFile.py

1
这比大多数投票答案都要好。它不仅检查语法,还显示所有未使用和未定义的变量。在运行时间较长的脚本时非常有帮助。 - Shashwat

15
python -m compileall -q .

将当前目录下的所有文件递归编译,并仅打印错误信息。
$ python -m compileall --help
usage: compileall.py [-h] [-l] [-r RECURSION] [-f] [-q] [-b] [-d DESTDIR] [-x REGEXP] [-i FILE] [-j WORKERS] [--invalidation-mode {checked-hash,timestamp,unchecked-hash}] [FILE|DIR [FILE|DIR ...]]

Utilities to support installing Python libraries.

positional arguments:
  FILE|DIR              zero or more file and directory names to compile; if no arguments given, defaults to the equivalent of -l sys.path

optional arguments:
  -h, --help            show this help message and exit
  -l                    don't recurse into subdirectories
  -r RECURSION          control the maximum recursion level. if `-l` and `-r` options are specified, then `-r` takes precedence.
  -f                    force rebuild even if timestamps are up to date
  -q                    output only error messages; -qq will suppress the error messages as well.
  -b                    use legacy (pre-PEP3147) compiled file locations
  -d DESTDIR            directory to prepend to file paths for use in compile-time tracebacks and in runtime tracebacks in cases where the source file is unavailable
  -x REGEXP             skip files matching the regular expression; the regexp is searched for in the full path of each file considered for compilation
  -i FILE               add all the files and directories listed in FILE to the list considered for compilation; if "-", names are read from stdin
  -j WORKERS, --workers WORKERS
                        Run compileall concurrently
  --invalidation-mode {checked-hash,timestamp,unchecked-hash}
                        set .pyc invalidation mode; defaults to "checked-hash" if the SOURCE_DATE_EPOCH environment variable is set, and "timestamp" otherwise.

当发现语法错误时,退出值为1。

感谢C2H5OH。


2

1

多亏了@Rosh Oxymoron提供的答案。我改进了脚本,扫描目录中所有的Python文件。因此,对于我们这些懒惰的人,只需提供目录,它就会扫描该目录中的所有Python文件。您可以指定任何文件扩展名。

import sys
import glob, os

os.chdir(sys.argv[1])
for file in glob.glob("*.py"):
    source = open(file, 'r').read() + '\n'
    compile(source, file, 'exec')

将此保存为checker.py并运行python checker.py ~/YOURDirectoryTOCHECK


0
由于某些原因(我是 Python 新手...),-m 调用没有起作用...
所以这里有一个 bash 包装器函数...
# ---------------------------------------------------------
# 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接