我需要从被调用者那里获取调用者信息(什么文件/什么行).我了解到我可以使用inpect模块来达到目的,但不是如何.
如何通过检查获得这些信息?或者有没有其他方法来获取信息?
import inspect
print __file__
c=inspect.currentframe()
print c.f_lineno
def hello():
print inspect.stack
?? what file called me in what line?
hello()
Run Code Online (Sandbox Code Playgroud) t1如果file 中有方法a.py并且有一个 file ,则从文件中b.py调用方法。如何获取方法内文件的完整/绝对路径?t1a.pyb.pyt1
使用检查模块(就像这里:how to get the caller's filename, method name in python),我可以获取文件的相对路径,但它似乎不包含绝对路径(或者还有一些其他属性对象,可以访问得到它?)。
举个例子:
a.py:
def t1():
print('callers absolute path')
Run Code Online (Sandbox Code Playgroud)
b.py:
from a import t1
t1() # should print absolute path for `b.py`
Run Code Online (Sandbox Code Playgroud)