开发者

How to change the file name runtime

开发者 https://www.devze.com 2023-03-19 18:14 出处:网络
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?

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消