Finding absolute path of imported module?

Discussion in 'Python' started by Roy Smith, Jun 19, 2012.

  1. Roy Smith

    Roy Smith Guest

    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.
    Roy Smith, Jun 19, 2012
    #1
    1. Advertising

  2. Roy Smith

    Gelonida N Guest

    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
    Gelonida N, Jun 19, 2012
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RonS
    Replies:
    5
    Views:
    4,408
  2. Dun Peal
    Replies:
    10
    Views:
    455
    Chris Rebert
    May 3, 2011
  3. Paul
    Replies:
    5
    Views:
    188
    Kyle Peterson
    Mar 20, 2006
  4. James Byrne
    Replies:
    3
    Views:
    550
    James Byrne
    Sep 14, 2010
  5. Volker Nicolai
    Replies:
    9
    Views:
    931
    Fabian Pilkowski
    Jul 4, 2005
Loading...

Share This Page