jupyter笔记本/实验室将当前目录设置为ipynb文件
所需的行为
我们在Vanilla Jupyter笔记本电脑/实验室中有现有的工作流程,我们使用相对路径来存储某些笔记本的输出。示例:
/home/user/notebooks/notebook1.ipynb
/home/user/notebooks/notebook1_output.log
/home/user/user/user/note/notebooks/project1/project。 ipynb
/home/user/notebooks/project1/project_output.log
在两个笔记本中,我们只需写入./ output.log
左右就产生输出。
但是
,我们现在正在尝试使用Jupyter可选组件尝试Google DataProc,并且当前目录始终为/
,无论其运行哪个笔记本。这同时适用于笔记本和实验室接口。
我尝试过
禁用c.filecontentsManager.root_dir ='/'
in /etc/jupyter/jupyter_note_notebook_config.py
导致当前目录设置为我启动<<<<<代码> jupyter笔记本来自,但它是总是 .ipynb 笔记本文件。
关于如何恢复“动态”当前目录行为的任何想法?
即使是不可能的,我也想了解DataProc如何使Jupyter的行为不同。
详细信息
- dataproc映像
2.0-debian10
- 笔记本服务器
6.2.0
- jupyterlab
3.0.18
Desired behaviour
We have an existing workflow in vanilla Jupyter Notebook/Lab where we use relative paths to store outputs of some notebooks. Example:
/home/user/notebooks/notebook1.ipynb
/home/user/notebooks/notebook1_output.log
/home/user/notebooks/project1/project.ipynb
/home/user/notebooks/project1/project_output.log
In both notebooks, we produce the output by simply writing to ./output.log
or so.
Problem
However, we are now trying Google Dataproc with Jupyter optional component, and the current directory is always /
regardless of which notebook it's run from. This applies for both the notebook and Lab interfaces.
What I've tried
Disabling c.FileContentsManager.root_dir='/'
in /etc/jupyter/jupyter_notebook_config.py
causes the current directory to be set to wherever I started jupyter notebook
from, but it is always that initial starting folder instead of following the .ipynb notebook files.
Any idea on how to restore the "dynamic" current directory behaviour?
Even if it's not possible, I'd like to understand how Dataproc even makes Jupyter behave differently.
Details
- Dataproc Image
2.0-debian10
- Notebook Server
6.2.0
- Jupyterlab
3.0.18
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,不可能始终在您的 .ipynb 文件中获取当前目录。 jupyter 从群集的主节点的本地
文件系统
运行。它将始终采用其内核的默认系统路径。在其他情况下(除了DataProc),也不可能始终获得Jupyter笔记本的路径。您可以查看此 thread> thread 关于此主题。
您必须提及将日志文件保存在所需路径中的目录路径。
请注意,实验室中的
GCS
文件夹是指群集的 Google cloud Storage Bucket 。您可以在GC中创建 .ipynb ,但是当您执行文件时,它将在本地系统内运行。因此,您将无法直接保存日志文件。编辑:
不仅是
dataproc
谁使jupyter
行为不同。如果您使用Google colab
笔记本您还将看到相同的行为。原因是因为您总是
在内核中执行代码
在文件所在的位置无关紧要。从理论上讲,多个笔记本电脑可以连接到该内核。因此,您不能为同一内核具有多个工作目录。正如我默认情况下提到的那样,如果您启动笔记本电脑,则将当前的工作目录设置为笔记本的路径。
链接到主线程 - &gt; https://github.com/ipython/ipython/ipython/ipython/ipython/issues/10123
No it is not possible to always get the current directory where your .ipynb file is. Jupyter is running from the local
filesystem
of the master node of your cluster. It will always take the default system path for its kernel.In other cases(besides dataproc) also it is not possible to consistently get the path of a Jupyter notebook. You can check out this thread regarding this topic.
You have to mention the directory path for your log file to be saved in the desired path.
Note that the
GCS
folder in your Lab refers to the Google Cloud storage Bucket of your cluster. You can create .ipynb in GCS but when you will execute the file it will be running inside the local system.Thus you will not be able to save log files in GCS directly.EDIT:
It's not only
Dataproc
who makesJupyter
behave differently.If you useGoogle Colab
notebooks there you will also see the same behaviour.The reason is because youre always
executing code in the kernel
does not matter where the file is. And in theory multiple notebooks could connect to that kernel.Thus you can't have multiple working directories for the same kernel.As I mentioned earlier by default if you're starting a notebook, the current working directory is set to the path of the notebook.
Link to the main thread -> https://github.com/ipython/ipython/issues/10123
绝对是大多数用例的一般解决方案似乎是GitHub问题中所描述的: https://github.com/ipython/ipython/issues/10123#issuecomment-354889020
Definitely a general solution for most use-cases seems to be what is described right here in the github issue: https://github.com/ipython/ipython/issues/10123#issuecomment-354889020