32

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()
loco.loop
  • 1,082
  • 1
  • 11
  • 22
  • Which path are you talking about, the URI for the notebook on the web or the path on disk? It looks like you want on disk and this is not reliably possible. – AChampion Aug 31 '18 at 16:38

6 Answers6

30

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.

Community
  • 1
  • 1
Jonas Adler
  • 9,571
  • 3
  • 41
  • 73
  • 7
    I feel like in 99.9999% of the case, just giving a way to return the location of the `.ipynb` would be sufficient. This feels like a surprising disconnect from real-world use, and to be non-pythonic thinking. – Jules G.M. May 05 '21 at 23:04
  • 1
    I think you're vastly underestimating how often this happens. Google Colab is very widely used and giving the location of the ipynb isn't possible there. – Jonas Adler May 10 '21 at 11:40
15

Combining the answers by the users @peng in the stackoverflow question:

path problem : NameError: name '__file__' is not defined

and the user @russel-dias in:

Find current directory and file's directory

you can get the path where the notebook resides by using:

os.path.dirname(os.path.realpath("__file__"))

so that ipynb_path = os.path.dirname(os.path.realpath("__file__"))

pristakos
  • 321
  • 2
  • 9
2

if you can open it you can use this function

1-open your Jupyter notebook 2- write this function 3-it will print out the path

pwd

if not navigate to your python installation folder open folder scripts and there you will find it.

hope this may help others

MoShamroukh
  • 649
  • 5
  • 7
  • 3
    :( This results to the root directory jupyter notebook started which may but is not always the directory the .ipynb resides.... – ntg Jan 07 '21 at 15:07
1

use this in cell

%%javascript
IPython.notebook.kernel.execute('nb_name = "' + IPython.notebook.notebook_name + '"')
print(nb_name)
Mark Rotteveel
  • 90,369
  • 161
  • 124
  • 175
-4

You can just use "pwd" which stands for print working directory. enter image description here

Milin
  • 1
  • 1
-4

You can right clic in the Jupyter notebook shortcut icon (in my case under Anaconda3 folder) and go to properties. There you will find the full path to Jupyter: D:\anaconda3\python.exe d:\anaconda3\cwp.py d:\anaconda3 d:\anaconda3\python.exe d:\anaconda3\Scripts\jupyter-notebook-script.py "%USERPROFILE%/"

Properties of Jupyter shortcut:

image

Shayan Shafiq
  • 1
  • 5
  • 17
  • 24
Andres
  • 3
  • 3