[英]Is there a NLP package or function that knows or can find locations from a document?
[英]A function that knows the name of the package from which it's been called
我有两个包:
my_tools
,包括函数f()
的系列工具 my_project
, my_tools
作为依赖项,并使用其f()
函数 我的问题是,当我从my_project
包的代码中调用f()
,我需要f()
才能找到它从my_project
包调用(例如返回包的名称)。
例如:
# my_project/code.py
from my_tools import f
print f() # prints 'my_project'
我一直在玩sys
和inspect
但到目前为止找不到任何解决方案。
使用inspect.currentframe
获取帧信息,然后检查模块的__package__
属性:
import inspect
def f():
frame = inspect.currentframe()
return frame.f_back.f_globals.get('__package__')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.