How can I import a script with an arbitrary name ?

Steven D'Aprano steve at
Tue Oct 31 13:17:37 CET 2006

On Tue, 31 Oct 2006 22:53:56 +1100, Ben Finney wrote:

> "Steven D'Aprano" <steve at> writes:
>> On Tue, 31 Oct 2006 11:00:52 +1100, Ben Finney wrote:
>> > 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 """
>> 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).

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.


More information about the Python-list mailing list