Python VSCode -- 如何设置调试工作目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38623138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VSCode -- how to set working directory for debug
提问by user1443098
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.
我开始将 vscode 用于 Python。我有一个简单的测试程序。我想在调试下运行它,我需要设置运行的工作目录。
How/where do I do that?
我该怎么做/在哪里做?
回答by The Red Pea
@SpeedCoder5 's comment deserves to be an answer;
@SpeedCoder5 的评论应该是一个答案;
Specifically, you can specify a dynamic working directory; (i.e. whichever directory where the currently-open Python file is located), using "cwd": "${fileDirname}"
具体来说,您可以指定一个动态工作目录;(即当前打开的 Python 文件所在的目录),使用"cwd": "${fileDirname}"
if you're using the Python: Current File (Integrated Terminal)
option when you run Python, your launch.json
file might look like mine, below.
如果您在Python: Current File (Integrated Terminal)
运行 Python 时使用该选项,则您的launch.json
文件可能与我的文件类似,如下所示。
{
"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; 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
文件控制 Visual Studio 代码项目的运行/调试设置;我的launch.json
文件是由 VS Code 自动生成的,位于我当前“打开项目”的目录中。我只是手动编辑了文件以添加"cwd": "${fileDirname}"
如上所示。
If you don't have a launch.json
file, try this:
如果您没有launch.json
文件,请尝试以下操作:
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.
要创建 launch.json 文件,请在 VS Code 中打开您的项目文件夹(文件 > 打开文件夹),然后选择调试视图顶部栏上的配置齿轮图标。
回答by Don
All you need to do is configure the cwd setting in launch.json file as follows:
您需要做的就是在 launch.json 文件中配置 cwd 设置,如下所示:
{
"name": "Python",
"type": "python",
"pythonPath":"python",
....
"cwd": "<Path to the directory>"
....
}
More information about this can be found on the official VS Code docs website.
可以在官方 VS Code 文档网站上找到有关此的更多信息。
回答by Xin
This setting helps me:
此设置帮助我:
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"cwd": "${workspaceFolder}\app\js", // set directory here
"program": "${workspaceFolder}\app\js\server.js", // set start js here
}
回答by CermakM
In some cases, it might be also useful to set the PYTHONPATH
along with the workspaceFolder
:
在某些情况下,将 与PYTHONPATH
一起设置可能也很有用workspaceFolder
:
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"env": {
"PYTHONPATH": "${cwd}"
}
}
回答by MJVM
I am posting this sample configuration for people who use TypeScript on Node.js
我正在为在 Node.js 上使用 TypeScript 的人发布此示例配置
in my project my Node.js server TypeScript files are located in folder Application_ts and the compiled js files are generated in the folder named Application
在我的项目中,我的 Node.js 服务器 TypeScript 文件位于文件夹 Application_ts 中,编译后的 js 文件在名为 Application 的文件夹中生成
because when we run our application in debug mode or start it normally we should start from Application folder which contains the js files so bellow configuration run debug from root folder where my application_ts also exists and works perfect
因为当我们在调试模式下运行我们的应用程序或正常启动它时,我们应该从包含 js 文件的应用程序文件夹开始,所以下面的配置从根文件夹运行调试,我的 application_ts 也存在并且工作完美
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug TypeScript in Node.js",
"program": "${workspaceRoot}\Application\app.js",
"cwd": "${workspaceRoot}\Application",
"protocol": "inspector",
"outFiles": [],
"sourceMaps": true
},
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"port": 5858,
"outFiles": [],
"sourceMaps": true
}
]
}
回答by Krzysztof Cieslak
You can set up current working directory for debugged program using cwd
argument in launch.json
您可以使用cwd
参数中的参数为调试程序设置当前工作目录launch.json