python - Assign into variable value from imported file -


i'm trying assign variable sql statement file imported. other filename: table1.py contain variable sql="select * column1" current script:

#!/usr/bin/python  datefrom = raw_input("please enter date yyyy-mm-dd: ") dateto = raw_input("please enter date yyyy-mm-dd: ") tablename = raw_input("please enter tablename: ") __import__(tablename) var= '%s.sql' % tablename print var 

all got is: "table1.sql"

so, imported first table name got input, trying put variable "var" value of "sql" variable table1.py file. of course want stay quite dynamic know because there more 1 sql file.

what doing wrong ?

while loading modules @ runtime need store module object variable , access variables normally.

table_module = __import__(tablename) table_sql = table_module.sql print table_sql 

now, if want modify statement, can use simple string manipulation in table_sql variable.


Comments