How to change the file name runtime
I have imported a file using the below command
code='IN'
exec "import %s_tmp_file" % code
Now i want to use this same imported file for other operations. How to give the file name in that case?
str = code_开发者_开发百科tmp_file.dict # this does not work.
Use the __import__
builtin instead of exec
:
my_module = __import__("%s_tmp_file" % code)
str = my_module.dict
See this page or help(__import__)
for more information.
Use __import__
(docs).
精彩评论