pychecker checks files by executing them ??

Skip Montanaro skip at pobox.com
Tue Jun 3 11:17:27 EDT 2003


    Axel> I got pychecker and installed it on my w2k machine as
    Axel> described. It also seems to work, but when I do: pychecker
    Axel> myFile.py it actually "executes" myFile.py, while checking it. Is
    Axel> this the correct behaviour??  Obviously I would like to check my
    Axel> scripts without executing them. Any idea what's going on ?

PyChecker imports the files you ask it to check.  If you want to provide the
ability to execute the file as a main application program, place the main
program code in a function and guard it.  For example, instead of:

    #!/usr/bin/env python

    import base64

    name = raw_input("Enter your name: ").strip()
    print "Hello %s." % base64.encodestring(name).strip()
    print "That's an unusual name.  Is it Dutch?"


try this:

    #!/usr/bin/env python

    import base64

    def main():
        name = raw_input("Enter your name: ").strip()
        print "Hello %s." % base64.encodestring(name).strip()
        print "That's an unusual name.  Is it Dutch?"

    if __name__ == "__main__":
        main()

As a pleasant side effect of this slight restructuring, your program might
run faster.  Why that's so is left as an exercise for the reader. :-)

Skip





More information about the Python-list mailing list