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

I used to use perl -c programfile to 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?


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

python -m py_compile script.py

You can use these tools:

  • PyChecker
  • Pyflakes
  • Pylint

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

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

    链接地址: http://www.djcxy.com/p/23876.html

    上一篇: Python中的UnboundLocalError

    下一篇: 我如何检查Python脚本的语法而不执行它?