在 Python 中,如何从导入的模块访问主模块的命名空间? [英] In Python, how can I access the namespace of the main module from an imported module?

查看:14
本文介绍了在 Python 中,如何从导入的模块访问主模块的命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体来说,我需要从导入模块的主模块中获取一些对象和全局变量.当父模块想要从子模块中获得某些特定的东西时,我知道如何找到这些东西,但我不知道如何朝另一个方向发展.

Specifically, I need to get at some objects and globals from the main module in an imported module. I know how to find those things when the parent module wants some particular thing from a child module, but I can't figure out how to go in the other direction.

推荐答案

import __main__

但不要这样做.

这篇关于在 Python 中,如何从导入的模块访问主模块的命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆