【问题标题】:Visual Studio Code - Activate Env before DebugVisual Studio Code - 调试前激活环境
【发布时间】:2023-03-09 16:10:01
【问题描述】:

我当前的设置涉及我编写的 boost-python 模块。为了调试这个模块,我编写了一个独立的 C++ 程序,它从 C++ 程序中调用 python 脚本。

int main()
{
    try
    {
        PyImport_AppendInittab("oum_export", INIT_MODULE);
        Py_Initialize();

        PyObject *obj = Py_BuildValue("s", "/media/thomas/Data/seafile/Seafile/TUG/ILearnHeart/Anisotropic\\ Diffusion/python/oum_cpp_opt.py");

        FILE *file = _Py_fopen_obj(obj, "r+");
        if (file != NULL) 
            PyRun_SimpleFile(file, "D:/seafile/Seafile/TUG/ILearnHeart/Anisotropic Diffusion/python/oum_cpp_opt.py");
        else
            cout << "Script file to execute not found" << endl;

    }
    catch( p::error_already_set ) 
    {
        PyErr_Print();
    }

    Py_Finalize();
}

这应该让我可以轻松地调试对 Python 模块的回调,这些回调是用 C++ 编写的。在调用 vscode-debugger 时,程序崩溃并出现错误

Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding

这是因为我不在正确的 anaconda 环境中。在启动 gdb 之前,如何告诉 visual-studio 代码进入正确的环境(即:“source activate aniso_diff && gdb oum_export_test”)?

这是我当前的 launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "oum_export_test",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/build_linux",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build oum standalone"
        }
    ]
}

我尝试将 activate 命令合并到构建 preLaunchTask 中,但似乎 vscode 为 gdb 调用了一个新的 shell。

【问题讨论】:

    标签: c++ visual-studio-code anaconda boost-python vscode-debugger


    【解决方案1】:

    这对你们大多数人来说可能是不费吹灰之力,但我刚刚发现最简单的解决方案是在同一个 shell 上调用 vscode 之前简单地激活所需的环境。

    【讨论】:

    • 不幸的是,如果您正在使用删除开发扩展,则很难。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多