Quickly Navigating Python Libraries With ctags

Quickly Navigating Python Libraries With ctags

A tutorial for using ctags to efficiently navigate Python libraries for data scientists.

A lot of the tips I provide here previously come this blog post and from the fast.ai course, so I would like to thank these authors for laying the groundwork! I also have to admit that I am definitely not an expert at VS Code, so suggestions/comments would be greatly appreciated. Okay lets get started!

  1. First, if you already do not have Python, install Python. I strongly recommend downloading Python through Anaconda.
  2. Second, download and install Visual Studio Code. Again, it is completely free and is compatible with either Windows, Linux or Mac, so this should be easy enough!
  3. Next, open Visual Studio Code. First, you are going to have to select Python as your interpreter. To do this type Ctrl+Shift+P and start typing “Python: Select Interpreter.” Then select the Python version and environment you want to load. Again, I often use fastai so I select the Python environment for fastai. If “Python: Select Interpreter” is not appearing, this means that Python is not installed on your system. This is an easy fix, so just follow the instructions here.

4. Now open the folder that contains the Python library that you want to easily explore (e.g., fastai). When you do this, VS Code is going to create a directory there labeled .vscode. This directory contains a JSON file labeled settings.json. Open up the settings.json file in VS Code (or any text editor). We will edit this file in a couple of steps.

5. Next, you will need to download the ctags package. Ctags generates an index (or tag) file of names found in source and headers files. This is how we can quickly search through our code, by creating these tags. You can download them from this GitHub site. If you are using Windows, you can click on releases, select the most recent version, and select the zip file to download. Unzip this zip file wherever you like, but I usually just put it in the .vscodedirectory.

6. Remember the settings.json file? You are going to need to edit it now. Already in the JSON should be a line that specifies the path to the Python executable. Now you are going to need to add a line specifying the path to the ctags.exe file. Create a new line and add the following line of code (replace LIBRARY_PATH with the path to your Python library or wherever you chose to place the ctags.exe file):

"python.workspaceSymbols.ctagsPath": "LIBRARY_PATH\.vscode\ctags-2018-11-06_0f27679c-x64\ctags.exe"

Your settings.json file should look like this:

7. Next, generate the tags! VS Code makes this really easy. All you have to do is type Ctrl + t. This should create a tags file, which contains all of the tags. If this does not work, open up the Terminal is VS Code, go to the Output tab, type Ctrl + t again, and see what the output says.

8. You can now navigate tags by typing Ctrl + t and typing names of classes and functions you would like to search. Easy enough!

What else can I do with VS Code?

The tags have been created and now are ready to be used. There are many other useful shortcuts in VS Code. Here are a few that I am shamelessly stealing from the fast.ai course:

  • Command palette: Ctrl-shift-p
  • Go to symbol: Ctril-t
  • Find references: Shift-F12
  • Go to definition: F12
  • Go back: Alt-left
  • Go forward: Alt-right
  • Hide sidebar: Ctrl-b
  • Zen mode: Ctrl-k,z

Source: towardsdatascience