ipynbname
The `ipynbname` library (current version 2025.8.0.0) provides a simple utility to retrieve the filename or full path of the current Jupyter notebook when executed within a browser-based Jupyter environment or VS Code. It was created to address the lack of a built-in, reliable method for accessing this information, especially for tasks like automating blog post publishing from notebooks. The project is actively maintained with a release cadence of typically once or twice a year.
Common errors
-
FileNotFoundError: Can't identify the notebook path.
cause This error typically occurs when `ipynbname` is unable to communicate with the Jupyter server or retrieve the necessary session information to determine the notebook's location. This can happen in specific JupyterHub setups, non-browser environments (like `nbconvert`), or when running in unsupported IDEs.fixEnsure the notebook is being run in a standard browser-based Jupyter environment or a explicitly supported VS Code setup. If using JupyterHub, verify its configuration or check for known compatibility issues on the `ipynbname` GitHub page. Avoid running `ipynbname` in headless or programmatic execution pipelines. -
IndexError: list index out of range
cause This error usually indicates that `ipynbname`'s internal mechanism for querying the Jupyter server for active sessions or notebook information received an unexpected or empty response, preventing it from parsing the expected data structure.fixRestarting the Jupyter kernel or the entire Jupyter server can often resolve transient `IndexError` issues. Ensure the notebook is saved and that the Jupyter environment is stable. If the problem persists, check for environment-specific bugs on the `ipynbname` GitHub repository.
Warnings
- gotcha `ipynbname` is primarily designed for interactive Jupyter notebook environments (browser or VS Code). It does not reliably function when notebooks are run programmatically or converted, such as with `nbconvert` or `papermill`.
- gotcha Jupyter server environments secured with a password may prevent `ipynbname` from correctly identifying the notebook's name or path.
- gotcha While `ipynbname` generally supports VS Code, specific configurations or versions might require a workaround for reliable operation.
Install
-
pip install ipynbname
Imports
- ipynbname
import ipynbname
Quickstart
import ipynbname
try:
# Get the notebook filename (e.g., "MyNotebook.ipynb")
nb_fname = ipynbname.name()
print(f"Notebook filename: {nb_fname}")
# Get the full path to the notebook
nb_path = ipynbname.path()
print(f"Notebook full path: {nb_path}")
except FileNotFoundError as e:
print(f"Error: {e}")
print("This usually means ipynbname couldn't identify the notebook. Ensure you are running it in a browser-based Jupyter environment or a compatible VS Code setup.")
except Exception as e:
print(f"An unexpected error occurred: {e}")