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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4284313/
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-08-18 15:03:50  来源:igfitidea点击:

How can I check the syntax of Python script without executing it?

pythoncompilationsyntax-checking

提问by Eugene Yarmash

I used to use perl -c programfileto check the syntax of a Perl program and then exit without executing it. Is there an equivalent way to do this for a Python script?

我曾经用来perl -c programfile检查 Perl 程序的语法,然后不执行就退出。是否有对 Python 脚本执行此操作的等效方法?

采纳答案by Mark Johnson

You can check the syntax by compiling it:

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

python -m py_compile script.py

回答by user225312

You can use these tools:

您可以使用以下工具:

回答by Rosh Oxymoron

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

Save this as checker.py and run python checker.py yourpyfile.py.

将其另存为 checker.py 并运行python checker.py yourpyfile.py.

回答by shakhmatov

Perhaps useful online checker PEP8 : http://pep8online.com/

也许有用的在线检查器 PEP8:http://pep8online.com/

回答by Yordan Georgiev

for some reason ( I am a py newbie ... ) the -m call did not work ...

出于某种原因(我是一个 py 新手......) -m 调用不起作用......

so here is a bash wrapper func ...

所以这里是一个 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

回答by jmd_dk

Here's another solution, using the astmodule:

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

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

To do it cleanly from within a Python script:

要从 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)

回答by Brian C.

Pyflakes does what you ask, it just checks the syntax. From the docs:

Pyflakes 会按照您的要求进行操作,它只是检查语法。从文档:

Pyflakes makes a simple promise: it will never complain about style, and it will try very, very hard to never emit false positives.

Pyflakes is also faster than Pylint or Pychecker. This is largely because Pyflakes only examines the syntax tree of each file individually.

Pyflakes 做出了一个简单的承诺:它永远不会抱怨风格,并且会非常非常努力地永远不会发出误报。

Pyflakes 也比 Pylint 或 Pychecker 快。这主要是因为 Pyflakes 只单独检查每个文件的语法树。

To install and use:

安装和使用:

$ pip install pyflakes
$ pyflakes yourPyFile.py