60

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,441
  • 1
  • 15
  • 27
  • 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
  • 1
    Also `pathlib.Path().cwd()` – SeF Sep 05 '22 at 14:46
  • The highest voted answers say you cannot reliably do so, but in many cases, you actually can: https://stackoverflow.com/questions/12544056/how-do-i-get-the-current-ipython-jupyter-notebook-name/. – phlummox Aug 23 '23 at 05:39

9 Answers9

49

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
  • 10,365
  • 5
  • 46
  • 73
  • 26
    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
  • 1
    I don't use colab and don't care about it. – tsbertalan Dec 29 '22 at 03:57
  • 1
    To elaborate, I don't use colab *because of this flaw*--I don't want to have to use flaky and special-purpose `drive.mount` code to access data files, save outputs and figures, etc. I consider their implementation of Jupyter broken because it creates this need for special-casing. – tsbertalan Dec 29 '22 at 04:09
8

Update:

I found this answer which solves the problem. The following command returns the folder path for both *.py and *.ipynb files.

import os
os.path.abspath("")

I have a bit of a work around to solve this issue but I find that it is often helpful to have for non-notebook projects as well. In the same folder as your notebook create a file called "base_fns.py". Inside this file place the following code:

import os

def get_local_folder():
    return os.path.dirname(os.path.realpath(__file__))

Then, you can get the path to the folder containing base_fns using:

from base_fns import get_local_folder()
rt_fldr = get_local_folder()
print(rt_fldr)

A few notes:

  1. This gives you the absolute path to the folder containing "base_fns.py", not your notebook. If your notebook and base_fns are in the same folder, then the absolute path to the folder for your notebook and base_fns will be the same.
  2. If you place base_fns in a different folder, you will need to know the relative path to navigate from the base_fns folder to your notebook folder.
  3. If you are working on a larger project with a folder structure, you can place base_fns.py in a known folder and then navigate around to find any other folders/files that you may require.
Braden Wiens
  • 117
  • 1
  • 5
3

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
  • 785
  • 7
  • 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
  • If you `chdir` anywhere in the notebook, this won't work. – Neinstein Aug 22 '22 at 15:12
3

In VSCode Jupyter, You can try this in a cell:

import IPython
notebook_name = "/".join(
        IPython.extract_module_locals()[1]["__vsc_ipynb_file__"].split("/")[-5:]
    )
print(notebook_name)
mallet
  • 2,454
  • 3
  • 37
  • 64
0

use this in cell

%%javascript
IPython.notebook.kernel.execute('nb_name = "' + IPython.notebook.notebook_name + '"')
print(nb_name)
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
0

What I am doing is not really beautifull but quite efficient. On VScode, I right click "Copy Path" on a sub folder in my working folder, in which I have my multiples Jupyter Notebook. I remove the end of the string and I obtain the aboslute path to the folder

I after use in one of my jupyter notebook the command: os.chdir(r"path_to_your_folder") and this is it.

NB: Since I work collaborately on repo cointaining Jupyter notebooks, this is the only ugly but efficient solution that I found.

Roms
  • 21
  • 6
-2

I know this is an old post, but it seems the path to the notebook can be found using os.path.abspath("mynotebook.ipynb")

It's hardcoding the name of the notebook, but that should be relatively easy to keep in sync.

EddieM
  • 1
  • 1
  • 2
    `os.path.abspath("mynotebook.ipynb")` just appends the file name to the current working directory. This is therefore not obtaining the path to the notebook if the directory was changed e.g. programmatically – Alexander Shekhovtsov Sep 07 '22 at 10:20
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 09 '22 at 14:56
-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,447
  • 5
  • 18
  • 25
Andres
  • 3
  • 3
-5

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

Milin
  • 1
  • 1