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

Piet van Oostrum piet at
Sun Jul 12 17:54:30 CEST 2009

>>>>> Stef Mientki <stef.mientki at> (SM) wrote:

>SM> Stef Mientki wrote:
>>> hello,
>>> when I''m working in a library,
>>> and want to test some of the library functions,
>>> I need to switch to  a main application,
>>> (which has a normal main-section)
>>> and run that.
>>> If the library is simply,
>>> I add a main section to the library,
>>> so no problem.
>>> But if the library requires a lot of overhead,
>>> I want to run some main program,
>>> that has a main section,
>>> and uses my library under construction.
>>> So how do I call from within the library module,
>>> in a main section in that library module,
>>> a main in another module ?
>>> thanks,
>>> Stef Mientki

>SM> btw,
>SM> I just found this in one of my programs (which works but isn't very
>SM> beautiful)

>SM> In the library fill under construction I put this main section:
>SM> ( is the main program, that has a main section and uses the
>SM> library under construction)

>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 ( '', new_globals )

Why not:

import db_test

I think that is what Aahz meant.

Piet van Oostrum <piet at>
URL: [PGP 8DAE142BE17999C4]
Private email: piet at

More information about the Python-list mailing list