有两个文件:检查谁进口我
# 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 3上再次使用Python 2文件名自省功能?