简体   繁体   中英

How to navigate to definitions of the base class in another file in PyCharm?

I have a project in the PyCharm editor in which the base class and the child class are implemented in separate files. In the child class I am accessing a function which is implemented in the base class.

Pycharm has got a keyboard shortcut "Ctrl + Alt + B" to go to the implementation of the method that is highlighted when the shortcut is pressed. However, I am not able to navigate to any implementation that is in a different file. I'm able to go only methods that are implemented within the same file. I also tried other options including navigating to the super method or navigating to the declaration just to see if any other command works for navigation into a different file but none of them seem to be effective.

Is there anything else that I have to do in order to make this possible apart from including the files within the same project in PyCharm? For example, is there any kind of indexing that I have to do before and so that PyCharm can recognise the implementation across different files?

Here are more details after a comment from Dan: The project structure is simple:

Project directory

  • Dir1
  • Dir2

Within Dir1

  • File1
  • File2

Withi Dir2

  • File3
  • File4

For the purposes of this question, File1 can be considered as base class and file3 as child class. When I am in File3 in pycharm and them at the place where a function implemented in file1 is being invoked, I am unable to navigate to that function in File1.

In fact, even if there is no class inheritance relationship, I am unable to navigate to any function which is implemented in a file other than the current file where the cursor is. For example, if I am at the invocation of the function in File3 and the function is in the same directory in File4, I'm unable to navigate to that function.

This is kind of surprising because the basic other IDE end possibly there is some issue in my settings but I cannot figure out what to do. Hopefully with this information, someone can at least point me in a direction to investigate further. I'm using the pycharm community edition

After days of searching, I was finally able to find the answer to the conundrum. When I was importing the base class module, I was importing it like this:

sys.path.insert(0, r"<relative path to base class>")
import base_class

It looks like pycharm does not understand this. When I changed this to

import dir1.file1

in dir2/file2 (which is the child class) the pycharm is able to navigate to the base class. I hope it helps someone. It took me literally three weeks to figure out. But I am not a very bright person, the others can either figure it out with the help of this answer or by themselves

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM