【问题标题】:Change current working directory in powershell from Visual Studio Code从 Visual Studio Code 更改 powershell 中的当前工作目录
【发布时间】:2021-11-01 18:32:37
【问题描述】:

使用 VS Code 调试 Python 时,我使用带有 PowerShell 的 Windows 终端作为我的外部终端。对于正常使用,我将 PowerShell 中的起始目录设置为 %USERPROFILE% (C:\Users\username) 但是在运行 python 代码时,我需要将起始文件夹更改为 %__CD__% (C:\Windows\system32) 以便当前工作目录将与我正在运行的 python 文件相同。 (由于测试,我发现这是它的工作方式。)在使用 python 处理文件时,这是必要的。不过,出于实用性和个人喜好,我不想将PowerShell设置中的startingDirectory设置改为%__CD__%

所以我的问题是,是否可以从 VSCode 临时设置 PowerShell 的startingDirectory 设置来运行代码?所以当我出于任何其他非调试原因打开 WT 时,起始目录是 %USERPROFILE%。

这些是我的 VS Code 启动配置:

"launch": {
    "configurations": [
            {
                "name": "Python: Integrated Terminal",
                "type": "python",
                "request": "launch",
                "program": "${file}",
                "cwd": "${fileDirname}",
                "console": "integratedTerminal"
            },
            {
                "name": "Python: External Terminal",
                "type": "python",
                "request": "launch",
                "program": "${file}",
                "console": "externalTerminal"
            }
    ]
},
"terminal.external.windowsExec":"wt -p cmd cmd",

这是我的 PowerShell 配置:

{
   "colorScheme": "One Half Dark",
   "commandline": "powershell.exe",
   "fontFace": "Consolas",
   guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
   hidden": false,
   name": "Windows PowerShell",
   startingDirectory": "%USERPROFILE%",
   tabTitle": "PowerShell"
}

感谢您的任何建议。

【问题讨论】:

  • 您可以打开包含您的代码的文件夹并将其保存为工作区。然后输入 CTRL+SHIFT+P -> “Workspace Settings (JSON)”,将该工作空间中“Launch”的 CWD 设置为目标目录

标签: powershell visual-studio-code vscode-debugger windows-terminal


【解决方案1】:

谢谢Michael Zla-dispute。发布您的建议作为帮助其他社区成员的答案。

您可以尝试以下两种方法来解决问题:

  1. 打开包含您的代码的文件夹并将其保存为工作区。然后输入CTRL+SHIFT+P -> "Workspace Settings (JSON)",将该工作区中“启动”的CWD设置为目标目录

  2. 配置 powershell 配置文件类型

    $profile 
    New-Item -path $profile -type file –force  
    

    在终端窗口中,它将为您提供 PowerShell 配置文件的链接 地点。打开该文件并添加

    set-location "file path, for example C:\Windows\system32"

    保存文件并重新加载 vscode!

您可以参考Managing Current LocationHow to quickly change shell folder to match the currently open filevscode does not set current directory in the terminal if a powershell $PROFILE also sets current directory

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 2011-03-29
    相关资源
    最近更新 更多