Python Forum

Full Version: How to you find the file where a class or a function was implemented?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have a big program where a python code calls several codes (written in Python and C++ ) and software. From function2 which is in my main program, I want to know where function1 where created knowing that function1 is called by function2. The function 1 might be available from an import, I guess. How do I print the name of the file in which the function 1 was implemented?

Thank you.
These functions are inside of .py files like MyOtherStuff.py
Just import the module where you need to use it:
import MyOtherStuff

then call function as MyOtherStuff.FunctionName()
The problem is that I don't know which module or file1.py, function1 is. Basically, I want to find in which file the called functions are?
you can get a list of all files in the directory using
os.listdir()
you can get a file extension with
filename, file_extension =  os.path.splittext(fullpath)
look only at files with extension py

then:
place all function names in a list
read each file in and check:
for item in list:
   if item in filebuffer:
       display filename and function name
Glob module can get all files by patern.

from glob import glob
py_files = list(glob("*.py"))