how to run the "main" section of another module ?

Emile van Sebille emile at fenx.com
Sun Jul 12 20:48:25 CEST 2009


On 7/12/2009 11:13 AM Stef Mientki said...
> 
>>> SM> if __name__ == '__main__':
>>> SM>    import db_test
>>> SM>    new_globals = {}
>>> SM>    new_globals [ '__name__' ] = '__main__'
>>> SM>    new_globals [ '__file__' ] = 'not really valuable'
>>> SM>    execfile ( 'db_test.py', new_globals )
>>>     
>>
>> Why not:

implied here is that you restructure...

>>
>> import db_test
>> db_test.main()
>>
>> I think that is what Aahz meant.
>>
>>   
> Yes I tried that too,
> but it gives the following error:
> "module object not callable"
> 
> 
> db_text.__main__()
> gives error:
> 'module' object has no attribute '__main__'
> 

You see, we're further assuming you've structured the module for this 
purpose.  IOW, your module should end with:

if __name__ == '__main__':
     main()

and nothing more.  Then, you can do:

import db_test
db_test.main()

Emile




More information about the Python-list mailing list