main logo
Subject: [dabo-dev] The problem with __name__
Author: Ed Leafe
Posted: 2004/10/31 15:21:26
 
View Entire Thread
New Search


I'm working now on getting py2exe and Inno Setup to create an
installable runtime engine for Windows. So far it's working well, but
there are some problems that have to do with the way programs are
written.

Most Python programs have code like the following:

if __name__ == "__main__":
do_something()

This code allows the script to be imported without running anything,
while still executing when run directly. While this is a cool design
for Python, it sucks for a runtime engine. Since the engine is what
Python thinks is the 'main' program, a program written with the above
code will do the following:

python myscript.py => executes do_something()
daborun myscript.py => does nothing

I've written into daborun the ability to pass a module name on the
command line, and daborun will call that module, so I *could* run the
code as follows:

daborun myscript.py do_something => executes do_something()

but this is a pain for most people. It also eliminates the possibility
of allowing the user to drag-and-drop the script to execute onto a
daborun icon, or associating .py files with daborun.exe. It also means
that scripts that don't have their execution code in a separate method,
but simply following the 'if __name__' test, cannot be run at all.

Is there any way to "fake" the value of __name__? I can change it in
daborun, but as soon as it imports the script, python sets it to the
name of the script.

___/
/
__/
/
____/
Ed Leafe
http://leafe.com/
http://dabodev.com/


 
©2004 Ed Leafe
<-- Prior Message New Search Next Message -->