import __main__ where can i find a module called "__main__.py"?

John Machin sjmachin at lexicon.net
Sun Aug 21 10:09:46 EDT 2005


wen wrote:
> and, in which case, the following case will happen:
> if __name__!='__main__':
>     do_sth()
> 
> any help would be appreciated.
> 
> 
Sorry, but your question is rather difficult to interpret.

For a start, you don't need/want to import __main__.

On import, __name__ is set to the name of the module. When a source file 
is run as a script, there is no module, so  __name__ is set to the dummy 
value "__main__". The module __main__ doesn't exist, and its source file 
__main__.py doesn't exist either (this side of the looking-glass).

Below is a minimal example of the behaviour of __name__.

C:\junk>type wenmain.py
print "__name__:", __name__

C:\junk>python
Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> import wenmain
__name__: wenmain
 >>> ^Z

C:\junk>python wenmain.py
__name__: __main__

Hope this helps,
John



More information about the Python-list mailing list