美文网首页
python2 从string中获取类,module

python2 从string中获取类,module

作者: bitingwind | 来源:发表于2020-06-25 22:47 被阅读0次

### 背景

经常需要从一个string 中,读取python的代码,然后解析拿来用。

详见:

https://stackoverflow.com/questions/5362771/how-to-load-a-module-from-code-in-a-string

```

#coding=utf-8

import sys,imp

a = 'a = 5'

a = imp.new_module('a')

key = '''13513'''

context.ret = action.sys_getFileBin(context, key)

exec context.ret in a.__dict__

print a.a

```

相关文章

网友评论

      本文标题:python2 从string中获取类,module

      本文链接:https://www.haomeiwen.com/subject/ebgouttx.html