Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17811 > unrolled thread

Get named module's file location

Started byGnarlodious <gnarlodious@gmail.com>
First post2011-12-23 08:01 -0800
Last post2011-12-23 22:39 +0000
Articles 9 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Get named module's file location Gnarlodious <gnarlodious@gmail.com> - 2011-12-23 08:01 -0800
    Re: Get named module's file location Roy Smith <roy@panix.com> - 2011-12-23 11:18 -0500
      Re: Get named module's file location Gnarlodious <gnarlodious@gmail.com> - 2011-12-23 08:51 -0800
        Re: Get named module's file location Roy Smith <roy@panix.com> - 2011-12-23 11:56 -0500
          Re: Get named module's file location Gnarlodious <gnarlodious@gmail.com> - 2011-12-23 11:40 -0800
            Re: Get named module's file location Chris Angelico <rosuav@gmail.com> - 2011-12-24 06:52 +1100
              Re: Get named module's file location Chris Angelico <rosuav@gmail.com> - 2011-12-24 08:21 +1100
            Re: Get named module's file location Roy Smith <roy@panix.com> - 2011-12-23 15:00 -0500
              Re: Get named module's file location Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-23 22:39 +0000

#17811 — Get named module's file location

FromGnarlodious <gnarlodious@gmail.com>
Date2011-12-23 08:01 -0800
SubjectGet named module's file location
Message-ID<32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23>
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

[toc] | [next] | [standalone]


#17813

FromRoy Smith <roy@panix.com>
Date2011-12-23 11:18 -0500
Message-ID<roy-533975.11181223122011@news.panix.com>
In reply to#17811
In article 
<32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23>,
 Gnarlodious <gnarlodious@gmail.com> 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?

[toc] | [prev] | [next] | [standalone]


#17815

FromGnarlodious <gnarlodious@gmail.com>
Date2011-12-23 08:51 -0800
Message-ID<4946660.379.1324659073535.JavaMail.geo-discussion-forums@prez5>
In reply to#17813
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

[toc] | [prev] | [next] | [standalone]


#17816

FromRoy Smith <roy@panix.com>
Date2011-12-23 11:56 -0500
Message-ID<roy-4780C2.11564723122011@news.panix.com>
In reply to#17815
In article 
<4946660.379.1324659073535.JavaMail.geo-discussion-forums@prez5>,
 Gnarlodious <gnarlodious@gmail.com> 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/).

[toc] | [prev] | [next] | [standalone]


#17820

FromGnarlodious <gnarlodious@gmail.com>
Date2011-12-23 11:40 -0800
Message-ID<4652751.858.1324669248908.JavaMail.geo-discussion-forums@prj1>
In reply to#17816
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

[toc] | [prev] | [next] | [standalone]


#17823

FromChris Angelico <rosuav@gmail.com>
Date2011-12-24 06:52 +1100
Message-ID<mailman.4038.1324669945.27778.python-list@python.org>
In reply to#17820
On Sat, Dec 24, 2011 at 6:40 AM, Gnarlodious <gnarlodious@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#17826

FromChris Angelico <rosuav@gmail.com>
Date2011-12-24 08:21 +1100
Message-ID<mailman.4040.1324675319.27778.python-list@python.org>
In reply to#17823
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 <gnarlodious@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#17824

FromRoy Smith <roy@panix.com>
Date2011-12-23 15:00 -0500
Message-ID<roy-8AEFBC.15001723122011@news.panix.com>
In reply to#17820
In article 
<4652751.858.1324669248908.JavaMail.geo-discussion-forums@prj1>,
 Gnarlodious <gnarlodious@gmail.com> 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))

[toc] | [prev] | [next] | [standalone]


#17828

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-12-23 22:39 +0000
Message-ID<4ef5033c$0$29973$c3e8da3$5496439d@news.astraweb.com>
In reply to#17824
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web