Find the location of a loaded module

Aaron Scott aaron.hildebrandt at gmail.com
Fri Feb 20 13:53:58 EST 2009


Okay, I'm going out of my mind. I have three directories -- 'act1',
'act2', and 'act3'. Each of these has a module named 'story'.

Through mod_python, I need to load 'story' in the directory 'act1'. I
do it like this:

	req.content_type = "text/html"
	sys.path.append(os.path.dirname( __file__ ))
	req.write(str(sys.path))
	import story
	req.write(story.game.Save())
	sys.path.pop()

According to sys.path, these are Python's paths:

['/usr/lib/python25.zip', '/usr/lib/python2.5', '/usr/lib/python2.5/
plat-linux2', '/usr/lib/python2.5/lib-tk', '/usr/lib/python2.5/lib-
dynload', '/usr/lib/python2.5/site-packages', '/usr/local/python2.5.2/
lib/python2.5/', '/home/www/---/docs/act1']

story.game.Save() returns the location of the story.game module, which
is reported as '/home/www/---/docs/act1/story/game.pyc'. So far so
good.

Now, I'll try the same thing from the 'act2' directory. These are the
paths reported in sys.path:

['/usr/lib/python25.zip', '/usr/lib/python2.5', '/usr/lib/python2.5/
plat-linux2', '/usr/lib/python2.5/lib-tk', '/usr/lib/python2.5/lib-
dynload', '/usr/lib/python2.5/site-packages', '/usr/local/python2.5.2/
lib/python2.5/', '/home/www/---/docs/act2']

All good, right? Not so fast. Here's what story.game.Save() returns as
its location: '/home/www/---/docs/act1/story/game.pyc'.

Which means it's loading the 'story' module from the old location,
even though that location is no longer in the path.

If I refresh a couple times, eventually it loads the module from the
proper directory ('act2'). Then, I'll go back to the first directory,
and find that it too will be loading the module from 'act2'. I'll
refresh, and it'll load from 'act1' again. I'll refresh a couple
times, and suddenly it's loading from 'act2' again.

I'm seriously going insane. If anyone has any insight, please, please
share it with me.

Aaron



More information about the Python-list mailing list