How can module determine its own path?

Steven D'Aprano steve at
Sat Oct 31 03:17:45 CET 2009

On Fri, 30 Oct 2009 20:08:28 -0500, Robert Kern wrote:

> Stef Mientki wrote:
>> but how do I configure a "properly initialized namespace dict"  (other
>> than my current namespace) ?
> filename = '...'
> ns = dict(
>      __file__=filename,
>      __name__='whatever_you_need_it_to_be', __builtins__=__builtins__,
> )
> execfile(filename, ns)

Or better still, don't use execfile at all, and just use the import 
system the way it's supposed to be used. If you find yourself doing by 
hand what the Python compiler normally does for you, it's a good sign 
that you're probably doing it wrong.


More information about the Python-list mailing list