Using VS Code to explore django and other library code - how do you do it?

Hi folks,

This is my first post here, so I hope this is on-topic. If not, if you can provide some links to where it makes sense to ask it i’ll ask there.

I’ve been using VS Code a while with Django, and on some projects, I can navigate source code in a project, of libraries I might import, which is incredibly helpful.

For example if I have some code here, I can sometimes right click on any of the imported libraries to see the underlying source code and if necessary, drop into a debugging session:

from django.views.generic import TemplateView
from rest_framework.authtoken.views import obtain_auth_token
from django.contrib.flatpages.views import flatpage

# some of my own code goes here using flatpage, for example

def some_method(blah):
    # not real code

    boop = blah.do_stuff()

    return flatpage(boop)

However, while I’m grateful when I can do it, I don’t understand what I need to have set up with VS Code, so I can consistently navigate source code of dependencies as well as my own code.

I understand it’s likely something to do with having a running language server, and the right virtual env, but it seemed like shaving yaks to dive into this myself without asking.

So here’s my question / call for help.

If you’re a django user, and you use VS Code, and you can reliably navigate the source code of libraries you depend on, without resorting to hacks like calling pipenv open library name in another session, can you please share your setup, or link to some docs you have found useful?

I’ve used django on and off since 2008, so I’m not new to the framework, but I’m a bit embarassed to say I’ve never found a setup I’m really happy with, and it seemed worth asking here as I can’t be the only person who:

  • a) likes using django
  • b) uses VS Code
  • c) finds it useful to navigate and if necessary edit the code of dependencies when investigating bugs during development

Thanks folks :heart:

P.S I’m aware of videos like this from justdjango, on VS Code setup. If it helps, I currently use pipenv for managing virtual environments, but, I’m pretty agnostic when it comes to venv management.

1 Like

It’s actually quite easy. You have a menu entry under “File”, labelled “Add folder to workspace”. I add my venv site-packages folder, and everything becomes immediately available to me.

3 Likes

Oh wow, it’s that simple? LOL.

Thanks Ken! OK, this works for making code appear in searches, but right clicking to ''go to definition" and so still seems a bit flaky.

My guess is there’s something magic I need to set up with intellisense for it all to behave as I want, but I have a good idea where to look:

And your tip has already created an improvement on what I had before :+1:

Yea, I don’t remember all the details about every setting, but I do seem to remember needing to specify which venv is being used in the workspace. I also seem to remember that it takes some time for it to scan all the libraries to figure out where things are. I believe it gets better & quicker over time.

Ken

With VS Code, a simple way to see the implementation of a Django function (for example) is to hold down the Cmd (Mac) or Ctrl (Windows/Linux) button and click the link that it exposes.

As an example, this is really useful when you want to inspect the library code when you’re overriding methods in class-based views and you don’t want to crack open CCBV.