[Tutor] '__name__' == '__main__'

Dave Angel d at davea.name
Tue Feb 21 08:01:11 CET 2012


On 02/20/2012 11:55 PM, Michael Lewis wrote:
> I am back to being confused. I just tried running the module without first
> importing it, and it worked just fine. How do I do this properly to where
> the module only runs if I import it?
>
I'd still like a definition of "just fine."  But anyway, your first 
problem is you somehow added extra quotes to a line that you had right 
in an earlier version.  Comparing two unequal literal strings will never 
prove true.  So the dependent clause never runs.

> <SNIP>
> if "__name__" == '__main__':
>      GetUserInput()
>
that should be
     if __name__ == "__main__":
                  do something
     else:
                     do something else

You'll have to decide which one you want to call GetUserInput() from.

You'll then have to decide if you really want to throw away the result.  
Just because IDLE dumps something out doesn't mean it'll get printed out 
when a person runs the program from a commandline.

At the very least, you want
     print GetUserInput()

although most of my earlier arguments might apply someday, once you're 
past this stuff.  Try inserting temporary prints into the code, so you 
can tell what has executed and what has not.



-- 

DaveA



More information about the Tutor mailing list