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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With