我有这样的结构:
merged.py:
everything
main.py:
import merged
sys.modules['othermodule'] = merged
othermodule.f()
如果我将它合并到单个文件 main.py
中,有什么方法可以告诉 othermodule
在 内部解析为
?main
>main.py
这种情况发生在使用amalgamate时package 将所有内容合并到单个文件中,但结果是两个具有上述结构的文件,我正在尝试将其合并为一个文件
最佳答案
sys.modules['othermodule'] = sys.modules[__name__]
获取您所在的当前模块并设置为其他模块。 现在您可以:
import othermodule
othermodule.f()
如果您更喜欢将模块分配给变量,您可以简单地:
othermodule = sys.modules[__name__]
othermodule.f()
关于python - 如何设置 sys.modules ["m"]=<当前模块>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38884630/