从字符串导入python模块

时间:2014-06-04 03:56:16

标签: python import module

据我所知,你可以使用 import 函数在中动态导入模块

>>sys = __import__("sys")

但是,如果你想在一个字符串中定义模块本身(而不仅仅是指定它的名字),我想这会是这样的

>>sketchy = importfromstring("def test(x):\n   print(x)\n");
>>sketchy.test("This is messed up")
This is messed up

我最感兴趣的是使用c api这样做,但标准的python显然也有帮助。

0 个答案:

没有答案