[Tutor] Opening a Qt .py file from within another python app

ken brockman krush1954 at yahoo.com
Thu Mar 22 20:39:55 CET 2012





________________________________
 
On 22/03/12 09:57, ken brockman wrote:

>> PS Another odd bit, was on the python docs page. It had said that using
>> import File_name, without the .py would import it, but not run it. Seems
>> a glaring oversight not to have mentioned, what would have made it run.

>Actually it does run it when you import, just not the bit after the

>if __name__ = "__main__".

>The point of modules is that you don't usually want them to run as a program, you import them so as to get access to the >functions within them. You want to control when they are called from your own code.

>-- Alan G


Thank you once more Alan, for taking the time to try to enlighten me. I had gone back to the python site, to try to get a better understanding of what it had said, with your insight in mind. But I think i am more befuddled now then i had been prior, If that is humanly possible. Below an excerpt from the python site, (2.7) but i think it is still applicable. 
------------------------------------------------

6.1.1. Executing modules as scripts
When you run a Python module with

python fibo.py <arguments>
the code in the module will be executed, just as if you imported it, but with the __name__ set to "__main__". That means that by adding this code at the end of your module:

if __name__ == "__main__":
    import sys
    fib(int(sys.argv[1]))
you can make the file usable as a script as well as an importable module, because the code that parses the command line only runs if the module is executed as the “main” file:

$ python fibo.py 50

If the module is imported, the code is not run:
----------------------------------------------------

Please bare with me, as I had said, I am a total novice. So if I understand this, which Is highly unlikely the,  If __name__==__main__, is what makes the module run as a stand alone script? Which shouldn't effect it's usability as a module which is imported? So why was it, that I couldn't get it to run without including that bit in my main python program. And I am assuming the line I had mistook to mean the module wouldn't run, means instead that the  "name ==main won't run, which once again leads to the question, then why would i need to include it to make it function??  

Do you see my point. What am i missing here? Some vital piece of the puzzle seems to be eluding me.

Ken
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120322/034c9093/attachment.html>


More information about the Tutor mailing list