syntax checker in python

Carl Banks pavlovevidence at
Sat Aug 8 09:30:06 CEST 2009

On Aug 7, 1:39 pm, horos11 <horo... at> wrote:
> ps - I just realized that it isn't enough to do:
> python -c 'import /path/to/script'
> since that actually executes any statement inside of the script
> (wheras all I want to do is check syntax)
> So - let me reprhase that - exactly how can you do a syntax check in
> python? Something like perl's -c:
>      perl -c script_name.p

A quick and dirty way would be to use the py_compile:

python -m py_compile /path/to/script

Warning: this simply appends "c" to the filename and writes out the
compiled file (kind of dumb behavior, actually), so if it's a script
you probably want to delete it afterwards:

python -m py_compile /path/to/script ; rm -f /path/to/scriptc

A little better might be to write a little Python helper script that
directs output to nowhere (something like this, feel free to

import py_compile

Carl Banks

More information about the Python-list mailing list