Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Finding absolute path of imported module?

Reply
Thread Tools

Finding absolute path of imported module?

 
 
Roy Smith
Guest
Posts: n/a
 
      06-19-2012
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.
 
Reply With Quote
 
 
 
 
Gelonida N
Guest
Posts: n/a
 
      06-19-2012
On 06/19/2012 09:32 PM, Christian Heimes wrote:
> Am 19.06.2012 19:55, schrieb Roy Smith:
>> 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.

>
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why do directly imported variables behave differently than thoseattached to imported module? Dun Peal Python 10 05-03-2011 10:11 PM
How does one get an absolute absolute file path? James Byrne Ruby 3 09-14-2010 06:02 PM
Finding absolute file path on server? Paul ASP General 5 03-20-2006 03:35 AM
"Variable ... is not imported..." using an imported variable from a module Volker Nicolai Perl Misc 9 07-04-2005 08:34 AM
Finding the absolute path to the root (or other known working directory)of a 'standard' J2EE web server. RonS Java 5 04-16-2005 03:30 AM



Advertisments