如何获得Jupyter Notebook的路径? [英] How to obtain Jupyter Notebook's path?

查看:191
本文介绍了如何获得Jupyter Notebook的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有获取笔记本路径的功能?

Is there a function to obtain a Notebook's path?

我已经对该主题进行了一些搜索,但是没有找到简单的方法...我想获取笔记本的路径,以便随后将其用于其他地方.这样,我可以在与笔记本相同的路径中保存/使用文件,而不必担心文件的保存位置.

I've Googled a little on the subject but didn't find a simple way to do it... I want to obtain the Notebook's path so I can then use it elsewhere. This way I could save/use files in the same path as the notebook without worrying about where it got saved.

现在,我的解决方案是将以下代码放在最上面,但显然,这至少带来了手动执行单元格的问题,而且如果工作目录发生更改,此操作将停止工作.

Right now my solution is to put the following code on top but obviously this poses at least the problem of manually having to execute a cell and also if the working directory changes this will stop working.

import os
current_path = os.getcwd()

推荐答案

TLDR:不能

不可能始终如一地获得Jupyter笔记本电脑的使用方法.有关更多信息,请参见 ipython问题#10123 .我将引用 Carreau :

以下是使用内核(在本例中为IPython)的原因:

Here are some reasons why the kernel (in this case IPython):

  • 可能不是从单个文件运行
  • 即使一个文件,该文件也可能不是笔记本.
  • 即使笔记本,笔记本也可能不在文件系统上.
  • 即使在文件系统上,它也可能不在同一台计算机上.
  • 即使在同一台机器上,文件的路径在IPython上下文中也可能没有意义.
  • 即使Jupyter协议并非旨在做到这一点.而且我们没有计划在短期或长期内更改这种抽象.

您的骇客在大多数情况下都可以使用,并且视情况而定还算不错.

Your hack works in most cases and is not too bad depending on the situation.

这篇关于如何获得Jupyter Notebook的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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