Can I import an arbitary file?
Peter Ballard
pballard at ozemail.com.au
Tue Feb 26 21:37:41 EST 2002
Fernando PĂ©rez <fperez528 at yahoo.com> wrote in message news:<a5esub$oau$1 at peabody.colorado.edu>...
> Peter Ballard wrote:
>
> > I've written a program (an SRAM generator, if you must know) which
> > has some configuration info (for different SRAM sizes etc.) in a separate
> > file. This configuration file is python code.
> >
> > What I want to do is import this file. The problem is, this file could
> > be any name at all. What I want to do is parse my command line, and
> > find out the name of my configuration file, and then import the file of
> > that name. Is that possible?
>
> look at __import__
Thank you. For posterity (and so everyone can have a good laugh
at my coding style), here's what I did...
Previously I used to copy the file (say, sram_8x8.py)
to configtmp.py and have the line "import configtmp" in my code.
Now, the following code appears to be completely equivalent:
##############
import imp
configfilename = "sram_8x8" # or whatever
fileinfo = imp.find_module(configfilename)
configtmp = imp.load_module("foo", fileinfo[0], configfilename, fileinfo[2])
##############
Thanks also to the other person who replied, but by the time
I saw that I already had my code working :)
Regards,
Peter Ballard
More information about the Python-list
mailing list