Finding absolute path of imported module?

R

Roy Smith

We're trying to debug a weird (and, of course, intermittent) problem a
gunicorn-based web application. Our production directory structure
looks like:

deploy/
rel-2012-06-14/
rel-2012-06-12/
rel-2012-06-11/
current -> rel-2012006-14

Each time we deploy a new version, we create a new release directory,
move the "current" symlink, and restart gunicorn. We've seen
instances where some of the workers end up importing some modules from
one directory and some from another (i.e. the old and new targets of
current).

So, the question is, is there any way to dump all the *absolute*
pathnames of all the imported modules? I can iterate over
sys.modules.values(), but that doesn't give me absolute pathnames, so
I can't tell which version of the symlink existed when the module was
imported.
 
G

Gelonida N

Am 19.06.2012 19:55, schrieb Roy Smith:

You can use os.path.abspath(module.__file__) to get the absolute path of
a module. This works reliable unless you use os.chdir() in your code.

abspath() may not normalize symlinks (not sure about it) but you can
check for symlink with os.path.islink() (uses os.lstat) and resolve the
link with os.readlink().
If I remember well, os.path.realpath(module.__file__) should normalize
the paths and resolve the symlinks
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top