python - VSCode -- 如何设置调试 Python 程序的工作目录

标签 python visual-studio-code

如何在 debug 下运行 Python 程序并设置运行的工作目录?

最佳答案

@SpeedCoder5's comment 值得回答。

launch.json 中,指定一个动态工作目录(即当前打开的 Python 文件所在的目录):

"cwd": "${fileDirname}"

这利用了 "variables reference" feature in VS Code 和预定义变量 fileDirname

如果您在运行 Python 时使用 Python: Current File (Integrated Terminal) 选项,您的 launch.json 文件可能与我的类似,如下所示 (more info on launch.json files here )。

{
    "version": "0.2.0",
    "configurations": [
    {
            "name": "Python: Current File (Integrated Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "cwd": "${fileDirname}"
    }, 

    //... other settings, but I modified the "Current File" setting above ...
}

Remember the launch.json file controls the run/debug settings of your Visual Studio code project ;我的 launch.json 文件是由 VS Code 在我当前的“打开项目”目录中自动生成的。我只是手动编辑文件以添加 "cwd": "${fileDirname}" 如上所示。

记住 launch.json 文件可能特定于您的项目或特定于您的目录,因此请确认您正在编辑 正确 launch.json (见评论)

如果你没有 launch.json 文件,try this:

To create a launch.json file, open your project folder in VS Code (File > Open Folder) and then select the Configure gear icon on the Debug view top bar.

关于python - VSCode -- 如何设置调试 Python 程序的工作目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38623138/

相关文章:

python - Django QuerySet.filter(property__regex ='^This not autocomplete' ) 的 VSCode 自动完成

python - % : 'NoneType' and 'int' 不支持的操作数类型

python - 在 Python 中引用子目录之间的文件的正确方法和 VSCode 设置是什么

python - 测试由于浮点限制而导致的舍入误差

python 导入文件时 php exec 失败

typescript - vscode 激活扩展失败 : Cannot find module with non-relative import

python - Python 注释是否必须与周围的代码块缩进相同? (VS代码)

vue.js - 在 VScode 和 Chrome 中调试 Vue,未绑定(bind)断点

python - Pandas 申请() : how to multiply selected columns based on string match and return complete dataframe

python - 在 anaconda 基础环境中安装软件包