[wxpython-users] 'if __name__' trick

Mark Erbaugh mark at microenh.com
Mon May 5 17:09:44 PDT 2008


On Tue, 2008-05-06 at 01:23 +0200, Chester wrote:
> Did any of you guys ever thought of trying (instead of  if __name ==
> "__main__") to try  if __name__ == __file__ ? Okay, I have tried it
> but it doesn't work and I can't figure it out why not. We check if the
> name of the module is the same as the name of the file is, so in the
> case of -- say myscript.py -- we check if myscript is the same as
> myscript.py. If you import a module named somemodule and do
> somemodule.__file__ that spits out  somemodule.py. So why does this
> thing not work in the case of    if __name == __file__ ? 

__name__ doesn't include the .py extension
__file__ may also include the path to the file

Hint:

Try importing test_if.py with the following content:

print '__name__: %s' % '__name__'
print '__file__: %s' % '__file__'


Also try running it directly (NameError)

Hope this helps,
Mark



More information about the wxpython-users mailing list