A Jupyter/IPython extension for clean, scoped project imports in notebooks.
Project description
relative-import-context
A Python utility and Jupyter/IPython extension for fixing notebook import issues without messy global sys.path hacks.
The problem
In Jupyter notebooks, this often fails:
from app.db import SessionLocal
Even when your project is structured correctly:
project/
app/
__init__.py
db.py
notebooks/
explore.ipynb
That happens because notebooks run inside an already-started Python interpreter. They do not behave like:
python -m app.module
#For regular apps, the better long-term fix is usually:
python -m app.module
or:
pip install -e .
But inside notebooks, developers often still need a clean way to add the project root temporarily.
Pip Install
From PyPI:
pip install relative-import-context
For local development:
pip install -e .
Jupyter usage
Load the extension:
%load_ext relative_import_context
Add your project root:
%relimport .
or from inside a notebooks/ folder:
%relimport ..
Then import normally:
from app.db import SessionLocal
Magic commands
%relimport . # Add one path
%relimports . ./libs # Add multiple paths
%relpaths # Show paths added by this extension
%relremove . # Remove one path
%relclear # Remove all paths added by this extension
%unload_ext relative_import_context
Context manager usage
You can also use it without Jupyter magic commands:
from relative_import_context import RelativeImportContext
with RelativeImportContext("."):
from app.db import SessionLocal
Capture logs during imports
import logging
from relative_import_context import RelativeImportContext
logger = logging.getLogger("my_app")
with RelativeImportContext(".", logger_name="my_app") as ctx:
logger.info("Importing app modules")
from app.db import SessionLocal
print(ctx.get_logs())
Why not just sys.path.append()?
sys.path.append() works, but it is global and easy to forget about. This package gives you either:
- notebook-friendly magic commands for path management
- a scoped context manager that restores
sys.pathafterward
Development
Test relative-import-context
git clone https://github.com/moyarich/relative-import-context.git
cd relative-import-context
python -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
pytest -s -v
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file relative_import_context-0.1.0.tar.gz.
File metadata
- Download URL: relative_import_context-0.1.0.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
31c7a8e4c89a931e04850509e4c479d9fb646c7fd3f9b2b15f0ed13e64eb851c
|
|
| MD5 |
7938acbc34c7109fdc56b77f0d79bb4a
|
|
| BLAKE2b-256 |
367d4a0bdd632388b8bdef42ab5f8788400d517e7f6dbfcac63c4301fb8c02d1
|
File details
Details for the file relative_import_context-0.1.0-py3-none-any.whl.
File metadata
- Download URL: relative_import_context-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e22aa91f4f61b5165335982b4d3965148d19b8061e9f1c85df1b3d65a0c03dc
|
|
| MD5 |
79cea898317a2cd8177111370c0aafae
|
|
| BLAKE2b-256 |
44bc9bcd17636306bbf59ba66ba74da0278289d905a9ce63e0adf343a1bf1504
|