Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pycharm find where Python function is called

Tags:

python

pycharm

Is there a way for PyCharm to show where a given Python function is called from?

I currently rely on simply searching for the function name across the project and this often works fine, but if a function name is vague there are a lot of incorrect hits. I'm wondering if I'm missing a feature somewhere, e.g. perhaps the search results could be further narrowed down to only show where modules import the module I'm searching from?

like image 885
Jon Lauridsen Avatar asked Jul 10 '12 22:07

Jon Lauridsen


People also ask

How do I get the details of a function in PyCharm?

Type Info In PyCharm, you can identify the type of an expression in the following way: Place the caret at the necessary code element and press Ctrl+Shift+P (or select View | Type Info from the main menu).

How do I find the python path in PyCharm?

Press Ctrl+Alt+S to open the IDE settings and select Project <project name> | Python Interpreter. Expand the list of the available interpreters and click the Show All link.

How do I find references in PyCharm?

From the main menu, choose Edit | Find Usages | Find Usages in File, or press Ctrl+F7 . The encountered usage is highlighted in the editor.


2 Answers

In PyCharm you can select a function and press Alt+Shift+F7 to run a usage search. It's also available under "Edit → Find → Find Usages". It looks like it's more intelligent than a text search.

Using static analysis to find where a function is called from is difficult in general in Python because it uses dynamic binding and has a lot of introspection so it's very easy to get false positives miss usages. In the case of module-level functions I think a good solution is to always use module.function to call the function and never do a from module import function. That way you can do a text search for 'module.function'. Python style guides generally recommend that you import functions etc. in this way so I think this is generally accepted good practice.

Finding method calls is of course much harder. One of the things I like about developing in Java and C# is being able to find all usages of a method by static analysis.

like image 116
Peter Graham Avatar answered Oct 02 '22 00:10

Peter Graham


Press the Ctrl key and simultaneously hover your mouse over the function header.The function name should get highlighted.Click on the function name to get a list of all instances where the function is called.

If you press the Ctrl key and simultaneously hover your mouse over a function call, then the function name will be highlighted and clicking on it will take you to the function definition.

like image 24
Chandana N T Avatar answered Oct 02 '22 01:10

Chandana N T