在 VS Code 中调试 youtube-dl 时导入错误

Import error when debugging youtube-dl in VS Code

我正在尝试为 youtube-dl 编写一个新的提取器。首先我想调试 __main__.py 以了解该工具,但我无法使用 VS Code 进行调试。这是我的 launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "args": ["a_youtube_video"]
        }
    ]
}

我的断点设置在__main__.py,看起来像这样:

from __future__ import unicode_literals

# Execute with
# $ python youtube_dl/__main__.py (2.6+)
# $ python -m youtube_dl          (2.7+)

import sys

if __package__ is None and not hasattr(sys, 'frozen'):
    # direct call of __main__.py
    import os.path
    path = os.path.realpath(os.path.abspath(__file__))
    sys.path.insert(0, os.path.dirname(os.path.dirname(path)))

import youtube_dl

if __name__ == '__main__':
    youtube_dl.main() # Breakpoint here    

我遇到的错误是 import youtube_dl 行,它报告没有名为 youtube_dl 的模块。我在这里错过了什么?

编辑:我刚刚找到了调试它的方法。它在__main__.py的评论中说得对:从2.7开始,程序必须运行作为一个模块。但是我还是不明白这个模块的东西。

尝试创建 "Python: Module" 调试配置。在那里你会在你的配置中看到一个 "module" 键,你可以指定适当的使用 __main__ (即我不知道是否有顶级 __main__.py 或者它是否在更改需要指定内容的包)。