1

Let's suppose we have the following Python script in my_script.py file:

import numpy as np
import matplotlib.pyplot as plt

a = np.linspace(1, 10, 100)

plt.figure()
plt.plot(a, a**2)
plt.show()

Is there a way to tell programmatically if (and in which line on the script) the function plot from the module matplotlib.pyplot is being called? [so that the answer will be something like 'line 7, column 1']

Ultimately, what I would like to do is to come up with a Python code that could locate, in another (given) Python script file, the position(s) in the file where a specific function from a specific module is being called.

I'm sure there are some Python tools for this, it is only that it's been hard for me to find anything on the web because I don't know the jargon for this kind of operation (introspection?) and basically don't know how to look for it. I guess something like modulefinder might work, but I have had no experience working with it and figure some of you guys might point me in the right direction.

Sorry for the bad english (not my native language). Thanks in advance for your help.

user2734434
  • 2,661
  • 2
  • 14
  • 16
  • I'm not sure why exactly you want to do this, but it might be useful to know pycharm has a lot of this sort of thing already built in – Nathan Jul 12 '20 at 19:52
  • https://stackoverflow.com/questions/55831844/find-all-usages-of-certain-methods-and-functions-in-a-python-package does this cover the question? – Nathan Jul 12 '20 at 20:39
  • Hey, thanks again @Nathan. I'll take a look at it and test it, then come back with an answer. Thanks again! – user2734434 Jul 12 '20 at 20:43

0 Answers0