Get named module's file location

Discussion in 'Python' started by Gnarlodious, Dec 23, 2011.

  1. Gnarlodious

    Gnarlodious Guest

    Given a module's name, how do I get the file path without importing it? Searched all over, can't find any such info.

    Is it possible to ask if a named module exists before attempting an import?

    Or are we forced to import first and catch any failure?

    -- Gnarlie
     
    Gnarlodious, Dec 23, 2011
    #1
    1. Advertising

  2. Gnarlodious

    Roy Smith Guest

    In article
    <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23>,
    Gnarlodious <> wrote:

    > Given a module's name, how do I get the file path without importing it?
    > Searched all over, can't find any such info.
    >
    > Is it possible to ask if a named module exists before attempting an import?
    >
    > Or are we forced to import first and catch any failure?
    >
    > -- Gnarlie


    import imp
    imp.find_module()

    Why do you want to do this?
     
    Roy Smith, Dec 23, 2011
    #2
    1. Advertising

  3. Gnarlodious

    Gnarlodious Guest

    Roy Smith wrote:

    > import imp
    > imp.find_module()


    Oh yeah that works. I am getting a list of modtimes using List Comprehension, from a list of modules, which will be compared to an older list to see if mod_wsgi needs to be restarted.

    Maybe thee is an easy way to get the modtimes, I'd be grateful.

    -- Gnarlie
     
    Gnarlodious, Dec 23, 2011
    #3
  4. Gnarlodious

    Roy Smith Guest

    In article
    <4946660.379.1324659073535.JavaMail.geo-discussion-forums@prez5>,
    Gnarlodious <> wrote:

    > Roy Smith wrote:
    >
    > > import imp
    > > imp.find_module()

    >
    > Oh yeah that works. I am getting a list of modtimes using List Comprehension,
    > from a list of modules, which will be compared to an older list to see if
    > mod_wsgi needs to be restarted.


    Ah, I see. Django's runserver does this. You might want to look to see
    how they implement it (https://www.djangoproject.com/download/).
     
    Roy Smith, Dec 23, 2011
    #4
  5. Gnarlodious

    Gnarlodious Guest

    I am rolling my own, and learning Python at the same time.

    One more question. Say I want to assemble a list of tuples like this:

    modules = ['wsgiref', 'http']
    import imp
    [(imp.find_module(module)[1], os.path.getmtime(imp.find_module(module)[1])) for module in modules]

    Can I in some way assign imp.find_module(module)[1] to a variable and reuse it? Is this a job for lambda?

    Thanks anyone.

    -- Gnarlie
     
    Gnarlodious, Dec 23, 2011
    #5
  6. On Sat, Dec 24, 2011 at 6:40 AM, Gnarlodious <> wrote:
    > [(imp.find_module(module)[1], os.path.getmtime(imp.find_module(module)[1])) for module in modules]
    >
    > Can I in some way assign imp.find_module(module)[1] to a variable and reuse it? Is this a job for lambda?


    Well, you can use an additional comprehension to provide a temporary
    variable, if you really want to do it all as a single expression.

    [(m, os.path.getmtime(m)) for m in (imp.find_module(module)[1] for
    module in modules)]

    ChrisA
     
    Chris Angelico, Dec 23, 2011
    #6
  7. Gnarlodious

    Roy Smith Guest

    In article
    <4652751.858.1324669248908.JavaMail.geo-discussion-forums@prj1>,
    Gnarlodious <> wrote:

    > I am rolling my own, and learning Python at the same time.


    Hmmm. The imp module is kind of deep magic for a first introduction to
    the language. But, whatever.

    > One more question. Say I want to assemble a list of tuples like this:
    >
    > modules = ['wsgiref', 'http']
    > import imp
    > [(imp.find_module(module)[1], os.path.getmtime(imp.find_module(module)[1]))
    > for module in modules]
    >
    > Can I in some way assign imp.find_module(module)[1] to a variable and reuse
    > it? Is this a job for lambda?


    I think what you want to do is rewrite the list comprehension as a
    regular loop.

    my_list = []
    for module in modules:
    m = imp.find_module(module)[1]
    my_list.append(m, os.path.getmtime(m))
     
    Roy Smith, Dec 23, 2011
    #7
  8. I'm guessing you meant for this to be on-list, and am hoping you don't
    mind that I'm replying on-list.

    On Sat, Dec 24, 2011 at 8:16 AM, Gnarlodious <> wrote:
    > Chris Angelico wrote:
    >> [(m, os.path.getmtime(m)) for m in (imp.find_module(module)[1] for
    >> module in modules)]
    >>
    >> Yeah, a little hard to read. Tell me, does this formulation execute
    >> imp.find_module(module) once or twice for each modname?


    What this does is save a temporary list, more or less. (It's actually
    a generator expression, not a list comprehension, but that's
    immaterial.)

    temporary = [imp.find_module(module)[1] for module in modules]
    [(m, os.path.getmtime(m)) for m in temporary]

    It iterates over modules, calling find_module for each, and saving the
    results to a new list. Then separately iterates over the new list,
    pairing each with the getmtime.

    Since I used parentheses instead of square brackets in the original
    expression, Python won't actually build the full list. Other than
    that, it's equivalent to the two-statement version, and you can try
    those two in IDLE to see what they do.

    ChrisA
     
    Chris Angelico, Dec 23, 2011
    #8
  9. On Fri, 23 Dec 2011 15:00:17 -0500, Roy Smith wrote:

    >> Can I in some way assign imp.find_module(module)[1] to a variable and
    >> reuse it? Is this a job for lambda?

    >
    > I think what you want to do is rewrite the list comprehension as a
    > regular loop.
    >
    > my_list = []
    > for module in modules:
    > m = imp.find_module(module)[1]
    > my_list.append(m, os.path.getmtime(m))


    +1


    List comprehensions are so cool that sometimes people forget that not
    every loop has to be a list comp. There is no shortage of newlines in the
    world, and not everything needs to be on a single line.


    --
    Steven
     
    Steven D'Aprano, Dec 23, 2011
    #9
    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. eric_bellard
    Replies:
    1
    Views:
    5,110
    CarlosRivera
    Oct 7, 2004
  2. emallove
    Replies:
    2
    Views:
    1,803
    Laurent Rahuel
    May 21, 2008
  3. process
    Replies:
    1
    Views:
    1,194
    Robert Kern
    Oct 17, 2008
  4. Mike Mimic

    Get module location from module

    Mike Mimic, Feb 22, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    84
    Ben Morrow
    Feb 22, 2004
  5. Alex Bird
    Replies:
    3
    Views:
    188
    Thomas 'PointedEars' Lahn
    May 9, 2008
Loading...

Share This Page