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

前端 未结 8 1068
长发绾君心
长发绾君心 2020-11-29 14:17

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 s

8条回答
  •  难免孤独
    2020-11-29 14:56

    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.

提交回复
热议问题