PyChecker must execute script?

Trent Mick trentm at
Thu Feb 6 00:09:05 CET 2003

[Just wrote]
> In article <mailman.1044481572.29181.python-list at>,
>  Trent Mick <trentm at> wrote:
> > [Geoff Gerrietts wrote]
> > > I believe PyChecker imports the modules, and analyzes the resulting
> > > bytecode.
> > 
> > Yes.
> What I don't understand is if it analyzes *byte code* then why it must 
> do a full import? Wouldn't a plain compile() suffice then? There must be 
> more to it.

It doesn't need to it just does for historical reasons. My understanding
is that the PyChecker folks are working on a version of PyChecker that
does not need to import the module. I am not involved though so don't
know the details.

> > To be more specific. If you put all top-level executing code within a
> >     if __name__ == "__main__":
> >         # top-level code here
> > block then you will be fine.
> A class or def statement is also top-level executing code, as are any 
> constant definitions and imports, so this definition is way to strict...

Unless I have been operating under gross misconceptions only top-level
code and "top-level class code" is executed on module import. For

    ------ --------------------------------
    # This code is executed on import
    foo = 1

    def spam():
        # The "def" statement is executed on import but the contents of
        # the routine are NOT.

    class Eggs:
        # The following two lines are executed on import
        foo = 1

        # Code inside method definitions are NOT executed om import
        def __init__(self):

    if __name__ == "__main__":
        # These lines are NOT executed on import
        foo = 1

In my experience this is not strict at all. In fact, this is
demonstrated by the usefulness of running PyChecker on the entire Python
Standard Library.


Trent Mick
TrentM at

More information about the Python-list mailing list