【发布时间】:2017-04-18 03:55:43
【问题描述】:
有两个文件:
# the_imported.py
import inspect
imported_by_fname = inspect.currentframe().f_back.f_code.co_filename
print('{} was imported by {}'.format(__name__, imported_by_fname))
还有:
# the_importer.py
import the_imported
使用 Python 2.7 执行时:
$ python the_importer.py
the_imported was imported by the_importer.py
使用 Python 3.5 执行时:
$ python3 the_importer.py
the_imported was imported by <frozen importlib._bootstrap>
<frozen importlib._bootstrap> 到底是怎么回事? import 和/或 inspect 发生了什么改变了这种行为?我们如何才能让 Python 2 文件名自省在 Python 3 上再次运行?
【问题讨论】:
标签: python python-3.x python-import introspection