Feature Spotlight: Navigating Python Code

Posted on by Dmitry Filippov

As programmers we read more than we write. And when projects we’re working on become larger and more complex, the problem of understanding and getting around unfamiliar or even your own code becomes more and more essential. The faster and easier you navigate through the code – the faster you can read it, understand its concepts and flows. Eventually this reflects on how productive you are in general.

Simple textual search is neither convenient nor effective when working with large projects. Sorting out thousands lines of Python code without special assistance can turn into an extremely difficult and time consuming task.

Here come different tools that make developer’s life much easier. It can be a plugin for a text editor such as ctags or vimpy for the Vim editor, or SublimeCodeIntel, SublimeRope, SublimePythonIDE, or whatever else for Sublime Text.

Using plugins for a text editor has several downsides, though. For example, if you want to turn Sublime or Vim into something that will provide you code completion, navigation, refactorings, add PEP8 checks or do linting for Python, you’ll have to install several additional plugins which won’t share information about the code with each other but rather maintain each own knowledge base.

Now, what if you use other languages besides Python in your project, such as JavaScript, HTML or even Django templates? You’ll have to find a particular plugin that supports your language. Hopefully this would work, however eventually you’ll end up with quite a messy arrangement – an editor with a bunch of plugins working independently and unreliably – hard to set up and a pain to maintain.

Fortunately, we’ve got PyCharm that makes things easy and smooth, boosting your productivity along the way. What distinguishes PyCharm the most is that being an IDE it seamlessly integrates different subsystems that are happy to work together, sharing data and source code knowledge between each other. This way PyCharm’s advanced code analysis, both static and dynamic, affects different subsystems, such as code completion, navigation, refactorings etc. The IDE knows everything about your code and you get the top-notch navigation solution that lets you focus on the actual development and makes you as fast and efficient as you can be.

Lets take a closer look at what PyCharm has to offer in regard to source code navigation:

1. Recent files
Most of the time you work with a finite set of files. It means you have to switch between them quickly all the time. The real time-saver here is the action called Recent files via Ctrl + E (Cmd + E for Mac). By default the focus is on the last accessed file. In the meantime you can open any tool window through this action:


Another useful feature similar to Recent files is Recently edited files, available via Shift + Ctrl + E (Shift + Cmd + E for Mac). This is the easiest way to get back to the code you are working on.

2. Structure
When you are not switching between files you’re probably navigating within one. The shortest way to do it is to press Ctrl + F12 (Cmd + F12 for Mac). The popup shows you the structure of file and allows you to quickly navigate to any item. Just as always you don’t need to touch your mouse:


Note that the speed search works here as well. Hit the shortcut twice to display inherited members.

3. Switcher
Just like the task switcher in an operating system, PyCharm provides a switcher between opened files and tool windows. The action is available via Ctrl+Tab:


4. Jump to navigation bar
Another useful feature worth to mention is Jump to navigation bar via Alt + Home (Alt + ↖ for Mac). The navigation bar at the top of the window is useless most of the time. That’s why developers usually hide it and call on demand via a shortcut.
The navigation bar supports speed search and other actions, such as code generations, documentation popup, etc:


5. Navigate between methods
If you need to jump over the methods quickly, just press Alt + ↑↓ (Ctrl + ↑↓ for Mac):


A similar action for navigating between braces is available via Ctrl + { and Ctrl + } (Cmd + { and Cmd + } for Mac).

6. Navigate to anything
The next feature, which is one of the most used in PyCharm, is Navigate to Class, File and Symbol.
This feature is available via Ctrl + N (Cmd + N for Mac) and supports sophisticated expressions, including camel humps, path, line navigate to, middle name matching, and many more. If you call it twice it shows you the results out of the project classes:


Just like for classes it works for files as well, via Ctrl + Shift + N (Cmd + Shift + N for Mac). The results are sorted by relevance to the expression:


And finally the most tricky feature is to navigate to a symbol via Ctrl + Alt + Shift + N (Alt + Cmd + Shift + N for Mac). The IDE constantly maintains the index of all symbols in the project (and within libraries as well) for quick resolve and navigation. You can type an expression and find a method or a field you need in a second:


7. Select in
If you need to open the file in a particular tool window (or Finder/Explorer) you can do it via Select In action by pressing Alt + F1:


8. Method Call hierarchy

Thanks to it’s fantastic static code analysis capabilities, PyCharm intelligently analyses the code and builds a call hierarchy trees for method calls in different scopes. Place the caret to a method name and press Ctrl + Alt +H (Cmd + Alt + H for Mac):


9. Other notable navigation features

  • Navigate to line number via Ctrl + G (Cmd + G for Mac)
  • Navigate to Custom Folding via Ctrl + Alt + Period
  • Related files via Gutter
  • Navigate to declaration via Ctrl + B (Cmd + B for Mac)
  • Navigate to implementations via Ctrl + Alt + B (Alt + Cmd + B for Mac)
  • Navigate to type declaration via Ctrl + Shift + B (Shift + Cmd + B for Mac)
  • Navigate to Super Method via Ctrl + U (Cmd + U for Mac)
  • Navigate to the last edit location via Shift + Ctrl + Backspace (Shift + Cmd + Backspace for Mac)
  • Navigate to the next error or warning via F2
  • Navigate to a related unit test via Shift + Ctrl + T (Shift + Cmd + T for Mac)
  • Navigate to a related symbol via Ctrl + Alt + Home (Cmd + Alt + Home)

All the navigational features mentioned here as well as other helpful functions are accessible via special Navigate system menu:


I hope you’ll enjoy these slick navigational features and save a great deal of time while at it.
And if you’d like to learn more about PyCharm’s code navigation capabilities, see our tutorial or online help.

Have a nice day!

Comments below can no longer be edited.

3 Responses to Feature Spotlight: Navigating Python Code

  1. tebu says:

    March 23, 2016

    Thnks for post. Finally I found what I require at single place.

  2. Eugene Zapolsky says:

    September 11, 2016

    Cool article. Thx.

  3. GD says:

    February 2, 2017

    Finally i found this post. Really helpful.


Subscribe for updates