Skip to content

Commit

Permalink
Fix: detect IPython and require magic for imports
Browse files Browse the repository at this point in the history
On IPython, the kernel adds $PWD to sys.path before proceeding
to import lots of modules. In order to remove the restriction
that user submodule notebook names not shadow existing libraries
we can remove $PWD from the sys.path under IPython.
Once the kernel has started, experienced users *might* expect $PWD
to be on sys.path. Literary only restores this for non-__package__
notebooks.  This would also work out-of-the-box for top-level
notebooks, but not inside e.g. examples/ folders.
  • Loading branch information
agoose77 committed May 12, 2022
1 parent 8b15c45 commit be98054
Show file tree
Hide file tree
Showing 24 changed files with 934 additions and 442 deletions.
14 changes: 1 addition & 13 deletions examples/absolute-importing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,6 @@
"We are going to use the _notebook_ hook, which adds support for importing other notebooks. This is different to the _module_ hook, which can be seen in [src/package_a/importer.ipynb](src/package_a/importer.ipynb)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "satisfactory-discretion",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"%load_ext literary.notebook"
]
},
{
"cell_type": "markdown",
"id": "favorite-questionnaire",
Expand Down Expand Up @@ -187,7 +175,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.4"
"version": "3.10.4"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion examples/src/package_a/__init__.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.4"
"version": "3.10.4"
}
},
"nbformat": 4,
Expand Down
4 changes: 2 additions & 2 deletions examples/src/package_a/docstring.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -38,7 +38,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.0"
"version": "3.10.4"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion examples/src/package_a/exports.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.4"
"version": "3.10.4"
}
},
"nbformat": 4,
Expand Down
34 changes: 1 addition & 33 deletions examples/src/package_a/importer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,6 @@
"## Loaded modules"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Loading the hook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First we need to run the shim that installs the import hook and loads the useful globals:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"%load_ext literary.module"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is **only needed if we wish to execute this notebook directly**, as the import hook performs this step automatically when importing notebooks. Defining `__package__` enables us to perform relative imports. Let's import a function from [exports.ipynb]():"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -408,7 +376,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.4"
"version": "3.10.4"
}
},
"nbformat": 4,
Expand Down
27 changes: 2 additions & 25 deletions examples/src/package_a/patching.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -226,29 +226,6 @@
"## `@patch` decorator"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `%load_ext literary.module` magic can be used to load a `patch` decorator from Literary:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"execution": {
"iopub.execute_input": "2021-05-18T21:00:06.297765Z",
"iopub.status.busy": "2021-05-18T21:00:06.297160Z",
"iopub.status.idle": "2021-05-18T21:00:06.442965Z",
"shell.execute_reply": "2021-05-18T21:00:06.441843Z"
}
},
"outputs": [],
"source": [
"%load_ext literary.module"
]
},
{
"cell_type": "code",
"execution_count": 10,
Expand Down Expand Up @@ -376,7 +353,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -390,7 +367,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.0"
"version": "3.10.4"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit be98054

Please sign in to comment.