如何使.py文件从文件目录而不是vscode中的工作区目录运行?

时间:2020-10-16 15:17:07

标签: python visual-studio-code vscode-settings

我正在使用vscode编写Python,当我运行.py文件时,它们是从工作区的目录中运行的。
如何从文件dricetory而不是工作区目录中运行该文件?

我尝试将其设置为:python.terminal.executeInFileDir,但设置没有任何改变。在https://code.visualstudio.com/docs/python/settings-reference上,他们对此的解释正是我要尝试做的,但没有用。
我还尝试了launch.json中的几个不同变量,例如"cwd": "{$fileDirname}"cwd": "{$file}"cwd": "{$fileDirname}
我的launch.json:

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "name": "Python: Current File",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "console": "integratedTerminal", 
        "cwd": "{$fileDirname}"
    }
]

}

1 个答案:

答案 0 :(得分:0)

当我在“ "python.terminal.executeInFileDir": true,”中设置settings.json并打开新的VSCode终端时,在执行文件时,VSCode会自动进入当前执行脚本所在的文件夹目录:

enter image description here