如何让 VS Code 中的 Code Runner 识别我当前的工作目录?

raj*_*ajn 1 visual-studio-code vscode-code-runner

我正在使用 VS Code,步骤如下:

  1. cd进入我的Dev文件夹
  2. my_projects然后在文件夹中写入代码

现在在my_projects文件夹内,它是根工作区目录,我可以导航到以下文件夹/文件:

my_projects/
        > DBHelper
            > dbhelper.py
            > config.ini
        > PortfolioManagement
        > Learning
Run Code Online (Sandbox Code Playgroud)

现在,当我运行 dbhelper.py 时,我尝试了以下测试:

import os
print(os.getcwd())
Run Code Online (Sandbox Code Playgroud)

我得到以下信息:

my_projects
Run Code Online (Sandbox Code Playgroud)

但我期待着

my_projects/DBHelper/
Run Code Online (Sandbox Code Playgroud)

如何让 VS Code 中的 Code Runner 识别我当前所在的文件(在本例中dbhelper.py位于当前工作目录中)?

我问的原因是因为我Database使用 postgres 创建了一个类,每当我dbhelper在不同的文件夹中使用时,它都无法识别该目录中的 config.ini 文件。

raj*_*ajn 5

我想到了。在我的 VS Code 设置中,我添加了以下设置:

{
    "python.pythonPath": "/Users/anaconda/bin/python",
    "code-runner.executorMap": 
    { 
        "python": "$pythonPath -u $fullFileName" 
    },
    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true,
    "python.terminal.executeInFileDir": true,
    "code-runner.fileDirectoryAsCwd": true
}
Run Code Online (Sandbox Code Playgroud)

  • `"code-runner.fileDirectoryAsCwd": true` 为我做了 (2认同)