【问题标题】:How to configure Visual Studio Code to debug Django app in a virtualenv?如何配置 Visual Studio Code 以在 virtualenv 中调试 Django 应用程序?
【发布时间】:2017-01-20 12:46:15
【问题描述】:

我希望能够在 Visual Studio Code 中对 Django 应用程序进行调试。我有一个 virtualenv,对 launch.json 文件进行了更改,如下所示:

{
    "name": "Django",
    "type": "python",
    "request": "launch",
    "stopOnEntry": true,
    "pythonPath": "${workspaceRoot}/.venv/bin/python2.7",
    "program": "${workspaceRoot}/mysite/manage.py",
    "args": [
        "runserver"
    ],
    "debugOptions": [
        "WaitOnAbnormalExit",
        "WaitOnNormalExit",
        "DjangoDebugging",
        "RedirectOutput"
    ]
},

在代码中放置几个​​断点并运行它。不幸的是,执行并没有停止在有断点的行上。我在没有 virtualenv 的情况下尝试了同样的方法,一切正常。

请指出我在这里做错了什么。

【问题讨论】:

    标签: python django visual-studio-code


    【解决方案1】:

    对我来说,以下 2 项更改有效

    1. 为pythonPath添加绝对路径
    2. 在启动项目时使用"--noreload" 选项

    这是我配置的相关部分

        {
            "name": "Django",
            "type": "python",
            "request": "launch",
            "stopOnEntry": true,
            "pythonPath": "/Users/xyz/Documents/dev/my_project/my_project_env/bin/python",
            "program": "${workspaceRoot}/manage.py",
            "args": [
                "runserver",
                "0.0.0.0:8080",
                "--noreload"                
            ],
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput",
                "DjangoDebugging"
            ]
        },
    

    【讨论】:

    • 对于窗口 pythonPath 将是(我的环境)"pythonPath": "c:\\Python3",
    • 我在运行 Django 应用程序的调试环境时遇到问题:我的 python 不在虚拟环境中,而我的 django 在。因此,我到处看到的解决方案是在 launch.json 设置中添加“pythonPath”行:“${workspaceRoot}/.venv/bin/python2.7”,不适合 - 我添加了更多细节 [stackoverflow.com/questions/68455529/…:
    【解决方案2】:

    1) 按 CTRL + ,
    2) 选择工作区设置
    3) 在打开的设置文件中添加以下行。

    "python.pythonPath": "path_to_your_env"
    


    大功告成!

    【讨论】:

    • 使用工作区设置的好点,因为您可以使用标准的 launch.json 文件
    猜你喜欢
    • 2017-04-12
    • 2022-07-22
    • 2016-10-12
    • 2016-04-01
    • 2019-04-19
    • 2022-06-15
    • 2016-08-31
    • 1970-01-01
    • 2018-12-02
    相关资源
    最近更新 更多