Skip to content

Instantly share code, notes, and snippets.

@jaseg
Created June 30, 2025 10:25
Show Gist options
  • Save jaseg/5107970f32f98f3b35d54e2aa4be0246 to your computer and use it in GitHub Desktop.
Save jaseg/5107970f32f98f3b35d54e2aa4be0246 to your computer and use it in GitHub Desktop.
Get the .ipynb notebook file path from a running Jupyter notebook

The way this works is that for each notebook, jupyter starts a python "kernel" process that actually runs the notebook's code. That kernel gets a json file with info on the notebook's location on the disk passed through its command line. Since we're running code in that exact python process, we can just grab that json file from sys.argv, and read it ourselves.

import sys
Path(json.loads(Path(sys.argv[-1]).read_bytes())['jupyter_session'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment