Wrong current working directory when running python code and jupyter extension in vscode

ara*_*lla 5 python jupyter visual-studio-code

I am trying to run python code using the jupyter extension in vscode here

I have a workspace open at this location /Users/user/Documents/ When I try and run the following code in a file called test.py in a child directory, the current working directory is set at the workspace level rather than the file. Is it possible to change a setting to use the cwd of the file, rather than the workspace? I cannot find one in settings.json and the "cwd" in launch.json only seems applicable to debugging.

File location: /Users/user/Documents/python_code/test.py

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

Expected output: /Users/user/Documents/python_code/

Actual output: /Users/user/Documents/

When running the same code through the terminal it prints the expected result, so the issue seems to be related to the jupyter extension

Ian*_*uff 7

对于您在 VS Code 中打开的任何特定文件夹/工作区,您可以使用 notebookFileRoot 设置来设置特定的绝对路径目录,以便在打开该文件夹的情况下启动交互式窗口时始终将 Jupyter 工作目录设置为。

notebookFileRoot 设置不支持始终在文件位置打开(无需将 notebookFileRoot 设置为每个文件夹的绝对路径)。${fileDirname} 等 VSCode 变量特定于任务和调试配置文件(launch.json 和 task.json)。我们专门添加了代码来识别我们的设置页面的 ${workspaceFolder},但我们无法识别其中的其他 VSCode 变量。

如果您愿意,这里有一个 github 项目建议使用此功能。如果您想要添加此功能,您可以关注它或投票。 https://github.com/Microsoft/vscode-python/issues/4441

2019 年 5 月 11 日编辑:我们现在进行了更改,允许将 VS Code 的“Notebook File Root”设置设置为 ${fileDirname}。此设置现在将启动相对于用于启动它们的文件位置的 Notebook Editor 和 Interactive Window 会话。

  • 使用远程容器时“Notebook File Root”不起作用的解决方案 (3认同)

归档时间:

查看次数:

4826 次

最近记录:

5 年,10 月 前