[Tutor] importing
Bob Gailer
bgailer@alum.rpi.edu
Wed Jun 25 09:23:01 2003
--=======268B3E9C=======
Content-Type: text/plain; x-avg-checked=avg-ok-13356CED; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 8bit
At 02:14 PM 6/24/2003 -0500, Isaac Hall wrote:
>alright, this is kind of an odd question, and Im not sure that there is
>another way I can go about doing this.
>
>I have a program that I would like to call with one or more than one
>configuration files in the command line. For what I want, the easiest way
>to create these configuration files was just to make them a collection of
>python dictionaries. This way I figured I wouldnt have to waste time
>creating some method to read a configuration file. However I found that
>putting in a statement like:
>
>try:
> import sys.argv[1]
>except:
> print 'bad config file'
>
>doesnt quite work. Does anyone know if there are clever ways to import a
>file that can be specified in the command line?
Unfortunately the language reference is not 100% precise in its explanation
of import. The word following import must be a module name (just as the
word following def must be a variable name to which the function is bound;
ditto for class classname). When the module name is stored as a string
you'd have to dynamically create and execute the import thus: exec "import
" + sys.argv[1].
Fortunately there is another way: __import__(sys.argv[1]). __import__ is
used (under the covers as it were) by the import statement.
Bob Gailer
bgailer@alum.rpi.edu
303 442 2625
--=======268B3E9C=======
Content-Type: text/plain; charset=us-ascii; x-avg=cert; x-avg-checked=avg-ok-13356CED
Content-Disposition: inline
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.492 / Virus Database: 291 - Release Date: 6/24/2003
--=======268B3E9C=======--