Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Get named module's file location

Reply
Thread Tools

Get named module's file location

 
 
Gnarlodious
Guest
Posts: n/a
 
      12-23-2011
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
 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      12-23-2011
In article
<32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23>,
Gnarlodious <(E-Mail Removed)> 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?
 
Reply With Quote
 
 
 
 
Gnarlodious
Guest
Posts: n/a
 
      12-23-2011
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
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      12-23-2011
In article
<4946660.379.1324659073535.JavaMail.geo-discussion-forums@prez5>,
Gnarlodious <(E-Mail Removed)> 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/).
 
Reply With Quote
 
Gnarlodious
Guest
Posts: n/a
 
      12-23-2011
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
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      12-23-2011
On Sat, Dec 24, 2011 at 6:40 AM, Gnarlodious <(E-Mail Removed)> 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
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      12-23-2011
In article
<4652751.858.1324669248908.JavaMail.geo-discussion-forums@prj1>,
Gnarlodious <(E-Mail Removed)> 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))
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      12-23-2011
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 <(E-Mail Removed)> 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
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-23-2011
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
 
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
Python in non-standard location erring with "No module named _sha256" emallove Python 2 05-21-2008 09:14 AM
Is it possible to get the text from browser location bar *before*navigation (e.g. not location.href) Alex Bird Javascript 3 05-09-2008 02:15 PM
Location, location, location =?Utf-8?B?VHJhY2V5?= Wireless Networking 2 02-17-2007 08:37 PM
after an image is moved to a new location, how to delete the image at the old location? zxo102 Python 0 08-01-2006 04:58 PM
How to trick a page into running at a location different from it'sphysical location? Luke Dalessandro ASP .Net 0 01-15-2006 05:59 AM



Advertisments