Python Forum
How to you find the file where a class or a function was implemented? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to you find the file where a class or a function was implemented? (/thread-2427.html)



How to you find the file where a class or a function was implemented? - MisterX - Mar-16-2017

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.


RE: How to you find the file where a class or a function was implemented? - Larz60+ - Mar-16-2017

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()


RE: How to you find the file where a class or a function was implemented? - MisterX - Mar-16-2017

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?


RE: How to you find the file where a class or a function was implemented? - Larz60+ - Mar-16-2017

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



RE: How to you find the file where a class or a function was implemented? - wavic - Mar-16-2017

Glob module can get all files by patern.

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