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

Eugene Yarmash picture Eugene Yarmash · Nov 26, 2010 · Viewed 216.3k times · Source

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?

Answer

Mark Johnson picture Mark Johnson · Dec 8, 2011

You can check the syntax by compiling it:

python -m py_compile script.py