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:
import sys
filename = sys.argv[1]
source = open(filename, 'r').read() + 'n'
compile(source, filename, 'exec')
将其保存为checker.py并运行python checker.py yourpyfile.py
。