question on "import __main__"

infidel saint.infidel at gmail.com
Fri Aug 26 11:09:41 EDT 2005


> import __main__
> if __name__!='__main__':
>    print 1
>
> print 2
>
> when i run test.py, i got
> 2
> on the screen.
>
> now, i have some question about the code, 1. since no __main__ module at
> all, why it's legal to write "import __main__"?

__main__ is the module that the interpreter starts executing.  So
"import __main__" is legal, though I'm guessing it doesn't really do
anything because the module is already loaded (since it's running).

> 2. since if running a script independently, the __name__ should be
> '__main__', why it's not in the above code?

But it is.




More information about the Python-list mailing list