syntax checker in python

H

horos11

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

Ed
 
J

Jonathan Gardner

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

You may want to read the sections under "Python Language Services" in
the Python Library Reference. There may be something you are looking
for there.
 
D

Diez B. Roggisch

horos11 said:
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:

See pylint & pychecker.

Diez
 
C

Carl Banks

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
embellish):

import py_compile
py_compile.compile(sys.argv[1],'/dev/null',None,False)



Carl Banks
 
N

nn

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

Ed

You might want to check PyFlakes; it doesn't execute scripts unlike
the others.

http://www.divmod.org/trac/wiki/DivmodPyflakes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top