VSCode -- 如何设置调试工作目录 [英] VSCode -- how to set working directory for debug
问题描述
我开始将 vscode 用于 Python.我有一个简单的测试程序.我想在调试下运行它,我需要设置运行的工作目录.
I'm starting to use vscode for Python. I have a simple test program. I want to run it under debug and I need to set the working directory for the run.
我该如何/在哪里做?
推荐答案
@SpeedCoder5 的评论值得一答;
@SpeedCoder5 's comment deserves to be an answer;
具体来说,可以指定一个动态的工作目录;(即当前打开的 Python 文件所在的目录),使用 "cwd":"${fileDirname}"
Specifically, you can specify a dynamic working directory; (i.e. whichever directory where the currently-open Python file is located), using "cwd": "${fileDirname}"
如果您在运行 Python 时使用 Python: Current File (Integrated Terminal)
选项,您的 launch.json
文件可能与我的类似,如下所示.
If you're using the Python: Current File (Integrated Terminal)
option when you run Python, your launch.json
file might look like mine, below.
{
"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 ...
}
记住 launch.json
文件控制 Visual Studio 代码项目的运行/调试设置;我的 launch.json
文件是由 VS Code 自动生成的,位于我当前的Open Project"目录中.我只是手动编辑了文件以添加 "cwd":"${fileDirname}"
如上所示.
Remember the launch.json
file controls the run/debug settings of your Visual Studio code project; my launch.json
file was auto-generated by VS Code, in the directory of my current "Open Project". I just edited the file manually to add "cwd": "${fileDirname}"
as shown above.
记住 launch.json
文件可能特定于您的项目或特定于您的目录,因此请确认您正在编辑正确 launch.json
(见评论)
Remember the launch.json
file may be specific to your project, or specific to your directory, so confirm you're editing the correct launch.json
(see comment)
如果您没有 launch.json
文件,试试这个:
If you don't have a launch.json
file, try this:
要创建 launch.json 文件,请在 VS Code 中打开您的项目文件夹(文件 > 打开文件夹),然后选择调试视图顶部栏上的配置齿轮图标.
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.
这篇关于VSCode -- 如何设置调试工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!