Get named module's file location

G

Gnarlodious

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
 
R

Roy Smith

Gnarlodious said:
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?
 
G

Gnarlodious

Roy said:
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
 
G

Gnarlodious

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
 
C

Chris Angelico

[(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
 
R

Roy Smith

Gnarlodious said:
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))
 
C

Chris Angelico

I'm guessing you meant for this to be on-list, and am hoping you don't
mind that I'm replying on-list.

Chris said:
[(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
 
S

Steven D'Aprano

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top