如何使用Visual Studio Code进入依赖代码? [英] How to step into dependency code using Visual Studio Code?

查看:621
本文介绍了如何使用Visual Studio Code进入依赖代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调试用户代码时,我想进入已安装的依赖项的代码,例如使用 pip install -e path / to / package



我试图在项目中找到依赖项的位置列出并可以浏览以打开源文件进行调试(例如,可以在PyCharm中通过外部库部分进行调试)。



我想进入

解决方案

内置调试配置应该可以实现。据我所知,唯一不可能的是Python依赖项是C代码(例如OpenCV,pygame),因为它们存储为 .so 文件而不是Python文件。



假设我有这样的结构:

  main 
└─ ─test.py
pkgs
└──mypkg
├──__init__.py
├──moduleA.py
└──setup.py

我是根据



然后启动调试器(按 F5 或从调试面板中选择它,然后按播放按钮)。当调试器在断点处停止时:



只需按 Step into 按钮(或 F11 )和VS Code应该将您带到外部依赖项的代码。您也可以直接在VS Code上打开文件,然后在其上放置断点。在编辑器上打开它之后,下次调试时,它将在这些断点处停止。




When debugging my user code, I want to step into the code of a dependency that is installed e.g. with pip install -e path/to/package.

I tried to find a place within the project where dependencies are listed and can be browsed to open the source file to debug (e.g. this is possible in PyCharm via the "External Libraries" section).

I would like to step into and through dependency code, but cannot find a way to do so.

解决方案

It should be possible with the built-in debugging configuration. As far as I know, the only time it's not possible is when the Python dependencies are C codes (ex. OpenCV, pygame) because they are stored as .so files instead of Python files.

Let's say I have this structure:

main
└── test.py
pkgs
└── mypkg
    ├── __init__.py
    ├── moduleA.py
    └── setup.py

I created mypkg based on Packaging Python Projects sample from the Python docs. I then installed it on my env using the same command you mentioned:

pip install -e /path/to/mypkg

In test.py I have this:

import moduleA
moduleA.add_two_num(1, 2)

First, make sure to set the VSCode interpreter to use the same env where you installed mypkg. See Select and activate an environment from the VSCode docs.

Next, create a debugging configuration for test.py:

{
    "name": "test",
    "type": "python",
    "request": "launch",
    "cwd": "${workspaceFolder}",
    "program": "/path/to/test.py",
    "pythonPath": "/path/to/.virtualenvs/test-py37/bin/python",
    "console": "integratedTerminal",
}

It is important here again to set pythonpath to point to the same python where you installed mypkg. Here I am using a virtualenv named test-py37.

Now, set a breakpoint on the line with the external package:

Then start the debugger (press F5 or select it from the Debug panel then press the Play button). When the debugger stops at the breakpoint:

Just press the Step Into button (or F11) and VS Code should take you to the code for the external dependency. You can also open the file directly on VS Code, then put breakpoints on them. Once it's open on your editor, the next time you debug, it stops on those breakpoints.

这篇关于如何使用Visual Studio Code进入依赖代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆