I have two packages:
my_tools
, a series tools including function f()
my_project
which has my_tools
as a dependency and which is using its f()
function My problem is that when I call f()
from my_project
package's code, I need f()
to be able to find it's been called from the my_project
package (and for instance return the package's name).
For example:
# my_project/code.py
from my_tools import f
print f() # prints 'my_project'
I've been playing around with sys
and inspect
but couldn't find any solution so far.
Use inspect.currentframe
to get frame information, then check __package__
attribute of the module:
import inspect
def f():
frame = inspect.currentframe()
return frame.f_back.f_globals.get('__package__')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.