How to run script from interpreter?

Steve Holden sholden at holdenweb.com
Fri Jan 19 05:34:03 EST 2001


"Dan Rolander" <dan.rolander at marriott.com> wrote in message
news:mailman.979898969.13587.python-list at python.org...
> This doesn't work for me. I know how to import and how to use the if
> __name__ trick, but how can I execute a script from the interactive Python
> interpreter? That is, can I load a script and have it run from inside the
> interpreter? (I'm sure this is easy I'm just not seeing it.)
>
> Dan

Well, if you've written your program as a .py file, the usual technique is
to enter an import statement to the interactive interpreter. The code is
executed as the import proceeds. The __name__ == _"__main__" trick is
usually used to run test code in a module intended primarily for import by
other code rather than for execution as a program.

Of course, this only works once, because further imports will assume they
can use teh already-imported module. To get subsequent modifications of the
script to work you have to use reload(mdulename), which will force
execution.

It's a slight pain that the first import is different from all the others,
but never mind.

regards
 Steve





More information about the Python-list mailing list