在vscode中运行python代码和jupyter扩展时当前工作目录错误 [英] Wrong current working directory when running python code and jupyter extension in vscode

查看:1115
本文介绍了在vscode中运行python代码和jupyter扩展时当前工作目录错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用vscode中的jupyter扩展名运行python代码此处

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

我在此位置打开了一个工作区 /用户/用户/文档/ 当我尝试在子目录中名为test.py的文件中运行以下代码时,当前的工作目录是在工作区级别而不是在文件级别设置的.是否可以更改设置以使用文件的cwd而不是工作区?我在settings.json中找不到一个,而launch.json中的"cwd"似乎仅适用于调试.

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.

文件位置: /Users/user/Documents/python_code/test.py

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

#%%
import os
print(os.getcwd())

预期输出: /Users/user/Documents/python_code/

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

实际输出: /Users/user/Documents/

Actual output: /Users/user/Documents/

通过终端运行相同的代码时,它会输出预期的结果,因此问题似乎与jupyter扩展有关

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

推荐答案

对于在VS Code中打开的任何特定文件夹/工作空间,可以使用notebookFileRoot设置来设置特定的绝对路径目录,以始终设置Jupyter工作目录打开该文件夹时启动交互式窗口"的时间.

For any specific folder / workspace that you have open in VS Code you can use the notebookFileRoot setting to set a specific absolute path directory to always set the Jupyter working directory to when you start the Interactive Window with that folder open.

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

Always opening on the file location (without having to set notebookFileRoot to an absolute path per folder) is not supported via the notebookFileRoot setting. The VSCode variables such as ${fileDirname} are specific to task and debug configuration files (launch.json and task.json). We've specifically added code to recognize ${workspaceFolder} for our settings page but we don't recognize other VSCode variables in there.

如果需要,这里有一个建议使用此功能的github项.如果要添加此功能,可以遵循或投票赞成. https://github.com/Microsoft/vscode-python/issues/4441

If you want there is a github item here that has suggested this feature. You can follow that or vote it up if you want this feature added. https://github.com/Microsoft/vscode-python/issues/4441

编辑11/5/2019: 现在,我们进行了更改,以允许将VS代码的笔记本文件根目录"设置设置为$ {fileDirname}.现在,相对于用于启动它们的文件位置,此设置将启动Notebook Editor(笔记本编辑器)和Interactive Window会话.

Edit 11/5/2019: We have now made a change to allow the VS Code "Notebook File Root" setting to be set to ${fileDirname}. This setting will now start up Notebook Editor and Interactive Window session relative to the file location that was used to start them.

这篇关于在vscode中运行python代码和jupyter扩展时当前工作目录错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆