Find the location of a loaded module

A

Aaron Scott

I'm running into a problem that's rapidly reaching keyboard-smashing
levels. I'm trying to import a module into Python, but it seems like
Python is almost randomly loading the module from an entirely
different directory, one that shouldn't be in the module search path.

When I tell Python to load a module, is there a way to tell which
directory the module was loaded from?
 
A

Aaron Scott

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
 
A

Aaron Scott

Here's another clue: if I'm trying to run the script from the
directory 'act1', but it's loading the module from 'act2', if I rename
the module directory in 'act2' and refresh, the module still reports
that it's running from '/home/www/---/docs/act2/story/game.pyc'...
even though that files no longer exists. If I refresh a few more
times, both 'act1' and 'act2' will load the module from 'act1's
directory (even though the directory doesn't appear in sys.path when
you try to load it from 'act2').

So, Python is trying to import a module from a directory that isn't in
sys.path, and will generally default to running the module from the
directory where it was last run. If I run it from 'act1', then 'act2',
both times it will load the module from 'act1'. If I do it the other
way around, it will load the module from 'act2' both times.

The question now is... why is it loading from a directory that isn't
in sys.path? How can I avoid this?
 
A

Aaron Scott

And more madness...

Executed from inside 'act1', which contains the directory / module
'story':


directory = os.path.dirname(__file__)
req.write(str(directory))
story = apache.import_module('story', path=[directory])


Results:


File "/home/www/---/docs/act1/play.py", line 24, in Rebuild
storylab = apache.import_module('story', path=[directory])

File "/usr/local/python2.5.2/lib/python2.5/site-packages/mod_python/
importer.py", line 304, in import_module
return __import__(module_name, {}, {}, ['*'])

ImportError: No module named story


Awesome. I'm going to go stick my head through a wall.
 
A

Aaron Scott

Son of a bitch. It gets worse.
Executed from inside 'act1', which contains the directory / module
'story':

        directory = os.path.dirname(__file__)
        req.write(str(directory))
        story = apache.import_module('story', path=[directory])

Results:

/home/www/---/docs/act1

  File "/home/www/---/docs/act1/play.py", line 24, in Rebuild
    storylab = apache.import_module('story', path=[directory])

  File "/usr/local/python2.5.2/lib/python2.5/site-packages/mod_python/
importer.py", line 304, in import_module
    return __import__(module_name, {}, {}, ['*'])

ImportError: No module named story

If I execute the exact same code from the 'act1' directory after
running it from the 'act2' directory, it successfully loads the
'story' module... from 'act2'. Even though I used the Apache importer
to specify the EXACT LOCATION of the module to import.

'req.write(str(os.path.dirname(__file__)))' returns '/home/www/---/
docs/act1'.

'req.write(story.game.Save())' returns '/home/www/--/docs/act2/
storylab/game.pyc' as the file being accessed.

BLOODY HELL.

Okay, deep breath.

Does anyone know what's going on? Am I just not understanding how
module importing in mod_python works? I'd really appreciate help,
since I'll be stuck at work today until I can get this sorted out, and
I've long since run out of ideas.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,276
Messages
2,571,384
Members
48,073
Latest member
ImogenePal

Latest Threads

Top