DEV Community

Cover image for Debugging a Pip Package in Visual Studio Code
Daniel Beadle
Daniel Beadle

Posted on

Debugging a Pip Package in Visual Studio Code

Let's consider the case where you're writing some incredibly important business logic in a simple environment and you want to see exactly what the pyjokes package is doing when you call get_joke().

business_logic.py:

from pyjokes import get_joke
print(get_joke())

The package's Git repository is often a great place to start, but sometimes you want to be able to step through the package.
Here's what you can do:

  1. Activate your virtual environment (if you haven't already)
  2. Uninstall the package pip uninstall pyjokes
  3. Reinstall the package from source with the editable flag
    pip install --editable git+git@github.com:pyjokes/pyjokes.git#egg=pyjokes
    
    • --editable can be shortened to -e
    • You aren't limited to Git repositories! You can also use "svn+" and "bzr+" and a few others. See Pip's VCS support documentation
    • #egg=[Custom Name] creates a directory [Custom Name].egg.info relative to the project path. You probably won't care about this, but it's required.
  4. The pyjokes repository can now be found in [virtual environment folder name] > src > pyjokes. But this isn't exactly what we're interested in.
  5. Find where the source code actually exists in the repository, in this case it's a subfolder with the same name. Sometimes it's a subfolder called "src". [virtual environment folder name] > src > pyjokes > pyjokes
  6. Move this folder up to the root level of your project. For example, this project directory now contains the following files and directories:
    • pyjokes (folder)
    • venv (virtual environment folder)
    • business_logic.py
  7. Almost done! Set the breakpoints you want in the pyjokes source code, open business_logic.py, switch to the VSCode debugger and press the green run button. Two prompts will appear, select Python for the first and "current file" for the second!

As an added bonus, you can edit the package too--good luck debugging!

Top comments (0)