New guy question

Alex Martelli aleaxit at yahoo.com
Sun Jun 10 03:33:25 EDT 2001


"Nick Perkins" <nperkins7 at home.com> wrote in message
news:YTDU6.180907$eK2.41575168 at news4.rdc1.on.home.com...
>
> ..of course you can also run your program from the >>> prompt:
> ( in addition to Alex's suggestion:  >>>execfile('alook.py')  )
>
> >>> import alook
>
> ..will effectively 'run' your program.
> ( actually it loads it into the current namespace, -- more or less the
same
> thing)
> ( note that you leave of the .py extension )
> The file alook.py must be on the sys.path -- another issue )

Also, if code in alook.py has a test such as:
    if __name__=='__main__':
        ...
it will work differently under import as opposed to execfile (or being
run at a shell prompt).  This is very handy, and highly idiomatic in
Python, too -- it lets you define an alook.py that defines functions,
classes &c which other modules can use, AND also may be run as
a stand-alone 'program' (but not with import:-).


Alex






More information about the Python-list mailing list