How To Find Where A Function Was Imported From In Python?
I have a Python module with a function in it: == bar.py == def foo(): pass == EOF == And then I import it into the global namespace like so: from bar import * So now the fu
Solution 1:
foo.__module__
should return bar
If you need more info, you can get it from sys.modules['bar']
, its __file__
and __package__
attributes may be interesting.
Solution 2:
Solution 3:
Instead of
from bar import *
use
from bar import foo
Using the from ... import *
syntax is a bad programming style precisely because it makes it hard to know where elements of your namespace come from.
Solution 4:
If you have IPython, you can also use ?
:
In[16]: runfile?
Signature: runfile(filename, args=None, wdir=None, is_module=False, global_vars=None)
Docstring:
Run filename
args: command line arguments (string)
wdir: working directory
File: /Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_umd.py
Type:function
Compare with __module__
:
In[17]: runfile.__module__
Out[17]: '_pydev_bundle.pydev_umd'
Post a Comment for "How To Find Where A Function Was Imported From In Python?"