Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain Jupyter Notebook's path?

Tags:

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() 
like image 482
loco.loop Avatar asked Aug 31 '18 16:08

loco.loop


1 Answers

TLDR: You can't

It is not possible to consistently get the path of a Jupyter notebook. See ipython issue #10123 for more information. I'll quote Carreau:

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

  • may not be running from single file
  • even if one file, the file may not be a notebook.
  • even if notebook, the notebook may not be on a filesystem.
  • even if on a file system, it may not be on the same machine.
  • even if on the same machine the path to the file may not make sens in the IPython context.
  • even if it make sens the Jupyter Protocol has not been designed to do so. And we have no plan to change this abstraction in short or long term.

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

like image 50
Jonas Adler Avatar answered Oct 06 '22 14:10

Jonas Adler