Find the location of a loaded module

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Feb 21 02:00:29 EST 2009


En Fri, 20 Feb 2009 20:44:21 -0200, Aaron Scott  
<aaron.hildebrandt at gmail.com> escribió:

> So, the problem lies with how Python cached the modules in memory.
> Yes, the modules were in two different locations and yes, the one that
> I specified using its direct path should be the one loaded. The
> problem is, the module isn't always loaded -- if it's already in
> memory, it'll use that instead. And since the modules had the same
> name, Python wouldn't distinguish between them, even though they
> weren't exactly the same.

Yes, that's how import works. It's barely documented, and you finally  
learned it the hard way...

> So, loading the module act1/story would load
> act1/story. Then, loading the module act2/story would use the story
> module already in memory. Of course, this made the problem hard to
> pinpoint, since memory is a fickle thing, and the results weren't
> always reproducible.
>
> The final solution? Renaming the 'story' modules to 'story_1' and
> 'story_2'... and importing them via 'exec("from story_"+modulename+"
> import game")'.
>
> Will I go to hell for this 'solution'? Probably. But hey, it means I
> can go home tonight instead of spending all evening at the office
> hitting my head against the wall. I'll come back to it Monday and try
> to figure out a more reasonable solution.

Use packages. Make act1 and act2 packages by creating __init__.py files.

-- 
Gabriel Genellina




More information about the Python-list mailing list