如何在 Python 中获取 __main__ 模块的文件名? [英] How to get filename of the __main__ module in Python?

查看:39
本文介绍了如何在 Python 中获取 __main__ 模块的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个模块:

a.py:

import b
print __name__, __file__

b.py:

print __name__, __file__

我运行a.py"文件.这打印:

I run the "a.py" file. This prints:

b        C:\path\to\code\b.py
__main__ C:\path\to\code\a.py

问题:如何从b.py"库中获取__main__ 模块(在本例中为a.py")的路径?

Question: how do I obtain the path to the __main__ module ("a.py" in this case) from within the "b.py" library?

推荐答案

import __main__
print __main__.__file__

这篇关于如何在 Python 中获取 __main__ 模块的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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