将任意Python源代码导入为Python模块
项目描述
stringimporter
stringimporter 允许您将原始字符串作为真正的Python模块导入
如何使用
您可以阅读测试(在tests目录中)或复制以下代码。
import stringimporter
module_from_thin_air = """
def multiply_by_2():
return lambda x: 2*x"""
loader, mymodule = stringimporter.import_str('yihaaa', module_from_thin_air)
hello_2 = mymodule.multiply_by_2()("hello ")
依赖项
无
兼容性
Python 3。我不知道(目前)如何在Python 2.7上模拟importlib.abc.SourceLoader。