![](/img/trans.png)
[英]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.