Jupyter Notebook的当前工作目录在vscode中设置为temp文件夹 [英] Current working directory for jupyter notebook sets to temp folder in vscode

查看:357
本文介绍了Jupyter Notebook的当前工作目录在vscode中设置为temp文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将当前工作目录(CWD)设置为VS Code中Jupyter Notebook的已编辑文件位置.我在python.dataScience.notebookFileRoot设置中使用${fileDirname}.但是,它将临时文件夹用作${fileDirname}而不是原始文件夹.

I'm trying to set current working directory (CWD) to edited file location for Jupyter Notebook in VS Code. I use ${fileDirname} in python.dataScience.notebookFileRoot setting. However it uses temporary folder as ${fileDirname} instead of original file folder.

已经多次讨论过同一问题(例如 https://stackoverflow.com/a/54794976/12488601 )指出了尝试的解决方案.

Same issue was discussed couple times already (e.g. https://stackoverflow.com/a/54794976/12488601) with tried solution pointed out.

以下是cwd的示例:

os.getcwd()
.. 'C:\\Users\\MjH\\AppData\\Local\\Temp\\1f6cc207-562f-4ae1-8754-e2013ae2c12d'

预期结果为C:\ Workspace \ Project.

While expected result is C:\Workspace\Project.

因此在我的情况下,无法使用${fileDirname}.我使用以下临时解决方案,很明显,如果移动文件,该解决方案将不会更新.

So use of ${fileDirname} does not work in my case. I use following ad-hoc solution, which, obviously, won't update if file is moved.

import sys
import os
sys.path.insert(0, r'C:\workspace\project')
os.chdir(sys.path[0])

现在我想了解三件事:

  1. 我的案子是独一无二的吗?
  2. 如果是一般性问题,是否有针对VS Code提交的功能请求/问题来解决?
  3. 是否有更好的临时解决方案?

VS代码版本:代码1.40.2(f359dd6,2019-11-25T14:54:45.096Z)
作业系统版本:Windows_NT x64 10.0.17763

VS Code version: Code 1.40.2 (f359dd6, 2019-11-25T14:54:45.096Z)
OS version: Windows_NT x64 10.0.17763

推荐答案

我也受到此问题的影响. 这是我解决的方法.

I'm also affected by this issue. Here is how I solved it.

Python>中使用${fileDirName}的配置参数; DataScience:Notebook File Root (笔记本文件根),具有这种效果,因为我可以在自己的环境中进行检查.

Using the configuration parameter of ${fileDirName} in Python > DataScience: Notebook File Root, has this effect as I could check in my environment.

如果我使用命令 Ctrl + Shift + P>打开Python交互式窗口, Python:显示"Python交互式窗口" ,然后运行:

If I open an Python Interactive Window using the commands Ctrl+Shift+P > Python:Show Python Interactive Window, and then run:

import os
os.getcwd()

输出是一个随机的临时文件夹.因此,我无法执行导入操作,导致找不到本地本地模块.

The output is a random temporal folder. So I couldn't do imports cause my local relative modules where not found.

但是,如果不是直接打开一个新的Python交互式窗口,而是运行我的任何python文件的代码单元格

However, If instead of opening directly a fresh Python Interactive Window I run a cell of code of any of my python files

#%%
print("Epstein didn't kill himself)

,然后找到当前的工作路径,它按预期更改为fileDirName.然后,与交互式窗口的任何交互都将正确执行(并且我的下一个导入将正确执行).

and then find the current working path, it changes to the fileDirName as expected. Then any interaction with the interactive window will perform correctly (and my next imports will perfom correctly).

为避免这种行为,并且由于我想在我的工作区文件夹中执行操作,我将 Jupyter:Notebook File Root (Jupyter:笔记本计算机文件根)更改为${workspaceFolder}.在这种情况下,打开一个新的Python交互式窗口,将自动将当前路径设置为workspaceFolder,而无需执行任何python单元.

To avoid this behaviour and because I would like to perform operations within my workspaceFolder I changed Jupyter: Notebook File Root to ${workspaceFolder}. In this case opening a new fresh Python Interactive Window, will set the current path automatically to the workspaceFolder, with and without the need to execute any python cell.

我认为,使用${fileDirName}时,最好将当前工作打开的文件夹设置为Python交互式窗口(Jupyter)的工作路径,而不是临时的随机文件夹(在不执行特定单元格的情况下打开的情况下),但看起来却不像这样.

I think that, when using ${fileDirName}, it would be good to set the current working open file folder as the working path of the Python Interactive Window (Jupyter) instead of the temporal random folder (In the case of opening without executing a specific cell), but doesn't look like it behaves like that.

希望它有用!

从2020年11月开始,Jupyter扩展与Python分离 VS Code的扩展.设置键已从重命名 python.data科学到jupyter

Since Nov 2020 the Jupyter extension is separated from Python extension for VS Code. The setting key has been renamed from python.data science to jupyter

这篇关于Jupyter Notebook的当前工作目录在vscode中设置为temp文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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