How can I import a script with an arbitrary name ?

L

leonel.gayard

Hi all,

I have a script responsible for loading and executing scripts on a
daily basis. Something like this:

import time
t = time.gmtime()
filename = t[0] + '-' + t[1] + '-' + t[2] + '.py'
import filename

So, I have a module with an arbitrary file name and I want to load it,
and later access its function definitions.
How can I do this ? In my example, the last line will obviously not
work.
 
R

Rob Williscroft

wrote in in
comp.lang.python:
Hi all,

I have a script responsible for loading and executing scripts on a
daily basis. Something like this:

import time
t = time.gmtime()
filename = t[0] + '-' + t[1] + '-' + t[2] + '.py'
import filename

So, I have a module with an arbitrary file name and I want to load it,
and later access its function definitions.
How can I do this ? In my example, the last line will obviously not
work.

http://docs.python.org/lib/built-in-funcs.html

The first one __import__ should do the trick.

Rob.
 
F

Fredrik Lundh

I have a script responsible for loading and executing scripts on a
daily basis. Something like this:

import time
t = time.gmtime()
filename = t[0] + '-' + t[1] + '-' + t[2] + '.py'
import filename

So, I have a module with an arbitrary file name and I want to load it,
and later access its function definitions.

execfile() is probably your best bet:

namespace = {}
execfile(filename, namespace)

namespace["function"](argument)

also see:

http://effbot.org/zone/import-string.htm#importing-by-filename

</F>
 
D

dakman

I had to do something like this a while back for a modular IRC bot that
I wrote.
__import__() will do the trick, however to avoid getting a cache of the
module I recomend doing something like...

mod = reload( __import__("%s-%s-%s" % ( t[0], t[1], t[2] ) ) )
 
B

Ben Finney

So, I have a module with an arbitrary file name and I want to load it,
and later access its function definitions.
How can I do this ? In my example, the last line will obviously not
work.

If you want a solution that gives you an actual module object, here's
what I use:

def make_module_from_file(module_name, file_name):
""" Make a new module object from the code in specified file """

from types import ModuleType
module = ModuleType(module_name)

module_file = open(file_name, 'r')
exec module_file in module.__dict__

return module
 
S

Steven D'Aprano

If you want a solution that gives you an actual module object, here's
what I use:

def make_module_from_file(module_name, file_name):
""" Make a new module object from the code in specified file """

from types import ModuleType
module = ModuleType(module_name)

module_file = open(file_name, 'r')
exec module_file in module.__dict__

return module

Isn't that awfully complicated? What's wrong with using __import__ to get
a module object?
<module 'math' from '/usr/lib/python2.4/lib-dynload/mathmodule.so'>


The only advantage (or maybe it is a disadvantage?) I can see to your
function is that it doesn't search the Python path and you can specify an
absolute file name.
 
F

Fredrik Lundh

Steven said:
The only advantage (or maybe it is a disadvantage?) I can see to your
function is that it doesn't search the Python path and you can specify an
absolute file name.

that's the whole point of doing an explicit load, of course. if you
think that's a disadvantage, you haven't done enough plugin work...

</F>
 
B

Ben Finney

Steven D'Aprano said:
The only advantage (or maybe it is a disadvantage?) I can see to
your function is that it doesn't search the Python path and you can
specify an absolute file name.

Which is exactly what the OP asked for (though he didn't necessarily
need a module object).
 
S

Steven D'Aprano

that's the whole point of doing an explicit load, of course. if you
think that's a disadvantage, you haven't done enough plugin work...

Guilty as charged.
 
S

Steven D'Aprano

Which is exactly what the OP asked for (though he didn't necessarily
need a module object).

I'm not arguing, you could very well be right, but I'm just curious what
part of the OP's post led you to believe he needed to specify an absolute
filename. Unless I'm missing a second post, he certainly never suggested
that his scripts weren't in the Python path, or that he couldn't add their
location to the path.

*shrug* It probably isn't important -- given the constraints as you read
them (extrapolated them?) your solution looks good.
 
F

Fredrik Lundh

Steven said:
I'm not arguing, you could very well be right, but I'm just curious what
part of the OP's post led you to believe he needed to specify an absolute
filename. Unless I'm missing a second post, he certainly never suggested
that his scripts weren't in the Python path, or that he couldn't add their
location to the path.

the fact that he's using dashes in the module name might be a hint, though.

in current versions, __import__ doesn't care as long as the file has
the right extension, but import requires valid Python identifiers, for
obvious reasons.

execfile doesn't care about any part of the filename, of course (and if
you replace it with exec, you don't even have to read the script from a
file).

</F>
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top