Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find where a function was imported from in Python?

Tags:

python

module

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 function foo is available to me. If I print it:

print foo

The interpreter happily tells me:

 <function foo at 0xb7eef10c>

Is there a way for me to find out that function foo came from module bar at this point?

like image 517
Stephen Gross Avatar asked Nov 13 '09 16:11

Stephen Gross


People also ask

How can I tell where Python is importing from?

Running "python -v"from the command line tells you what is being imported and from where. This is useful if you want to know the location of built in modules.

Where does import import from Python?

The Python import statement imports code from one module into another program. You can import all the code from a module by specifying the import keyword followed by the module you want to import. import statements appear at the top of a Python file, beneath any comments that may exist.

How do you check a library function in Python?

We can list down all the functions present in a Python module by simply using the dir() method in the Python shell or in the command prompt shell.


1 Answers

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.

like image 169
Wim Avatar answered Oct 21 '22 03:10

Wim