syntax checker in python

Discussion in 'Python' started by horos11, Aug 7, 2009.

  1. horos11

    horos11 Guest

    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
    horos11, Aug 7, 2009
    #1
    1. Advertising

  2. On Aug 7, 1:39 pm, horos11 <> 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
    >


    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.
    Jonathan Gardner, Aug 7, 2009
    #2
    1. Advertising

  3. horos11 schrieb:
    > 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
    Diez B. Roggisch, Aug 8, 2009
    #3
  4. horos11

    Carl Banks Guest

    On Aug 7, 1:39 pm, horos11 <> 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
    embellish):

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



    Carl Banks
    Carl Banks, Aug 8, 2009
    #4
  5. horos11

    nn Guest

    On Aug 7, 4:39 pm, horos11 <> 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
    >
    > Ed


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

    http://www.divmod.org/trac/wiki/DivmodPyflakes
    nn, Aug 10, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert
    Replies:
    6
    Views:
    2,648
    Jim Michaels
    Feb 3, 2006
  2. neerashish
    Replies:
    2
    Views:
    286
  3. Kinokunya
    Replies:
    3
    Views:
    409
  4. Pager O Rama

    MSN BLOCK CHECKER-MSN STATUS CHECKER-MSN PROBLEMS

    Pager O Rama, Apr 4, 2006, in forum: ASP General
    Replies:
    0
    Views:
    235
    Pager O Rama
    Apr 4, 2006
  5. Jacob Grover
    Replies:
    5
    Views:
    308
    Jacob Grover
    Jul 18, 2008
Loading...

Share This Page