[英]How to set sys.modules[“m”]=<current module>
我有這個結構:
merged.py:
everything
main.py:
import merged
sys.modules['othermodule'] = merged
othermodule.f()
如果我將它合並到單個文件main.py
,是否有任何方法可以在main.py
內部告訴othermodule
解析為main
?
當使用amalgamate包將所有內容連接成單個文件時會發生這種情況,但結果是兩個文件具有上面的結構,我試圖將其歸結為一個文件
sys.modules['othermodule'] = sys.modules[__name__]
獲取您當前的模塊並將其設置為othermodule。 現在你可以:
import othermodule
othermodule.f()
如果您更喜歡將模塊分配給變量,您可以簡單地:
othermodule = sys.modules[__name__]
othermodule.f()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.