[llvm] 535693f - [llvm][TableGen][Jupyter] Record current python when kernel is installed

David Spickett via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 5 00:58:54 PDT 2023


Author: David Spickett
Date: 2023-07-05T08:58:49+01:00
New Revision: 535693f8f74d539a4b34f2d226cdef8d71405d47

URL: https://github.com/llvm/llvm-project/commit/535693f8f74d539a4b34f2d226cdef8d71405d47
DIFF: https://github.com/llvm/llvm-project/commit/535693f8f74d539a4b34f2d226cdef8d71405d47.diff

LOG: [llvm][TableGen][Jupyter] Record current python when kernel is installed

Previously the kernel.json would always point to `python3` even if you
installed using a python from a virtualenv. This meant that tools like VSCode
would try to run the kernel against the system python and fail.

Added a note to the readme about it. I've removed the need to
add to PYTHONPTHON as well, turns out it wasn't needed.

This fixes an issue reported in https://discourse.llvm.org/t/tablegen-the-playground-ipynb-file-is-not-working-as-expected/71745.

Reviewed By: awarzynski

Differential Revision: https://reviews.llvm.org/D154351

Added: 
    

Modified: 
    llvm/utils/TableGen/jupyter/README.md
    llvm/utils/TableGen/jupyter/tablegen_kernel/install.py

Removed: 
    llvm/utils/TableGen/jupyter/tablegen_kernel/assets/kernel.json


################################################################################
diff  --git a/llvm/utils/TableGen/jupyter/README.md b/llvm/utils/TableGen/jupyter/README.md
index 660b5a7015feb7..f2909265d99c68 100644
--- a/llvm/utils/TableGen/jupyter/README.md
+++ b/llvm/utils/TableGen/jupyter/README.md
@@ -15,15 +15,13 @@ that is not possible, there are Markdown versions next to the notebook files.
 
 ## TableGen Kernel
 
-To use the kernel, first install it into jupyter:
+To use the kernel, first install it into jupyter.
 
-    python3 -m tablegen_kernel.install
-
-Then put this folder on your PYTHONPATH so jupyter can find it:
+If you have installed Jupyter into a virtual environment, adjust `python3` to
+be the interpreter for that environment. This will ensure that tools run the
+kernel in the correct context.
 
-```shell
-    export PYTHONPATH=$PYTHONPATH:<path to this dir>
-```
+    python3 -m tablegen_kernel.install
 
 Then run one of:
 

diff  --git a/llvm/utils/TableGen/jupyter/tablegen_kernel/assets/kernel.json b/llvm/utils/TableGen/jupyter/tablegen_kernel/assets/kernel.json
deleted file mode 100644
index 656fcbf4b379a5..00000000000000
--- a/llvm/utils/TableGen/jupyter/tablegen_kernel/assets/kernel.json
+++ /dev/null
@@ -1,14 +0,0 @@
-{
-    "argv": [
-        "python3", "-m", "tablegen_kernel", "-f", "{connection_file}"
-    ],
-    "display_name": "LLVM TableGen",
-    "language": "tablegen",
-    "language_info": {
-        "name": "tablegen",
-        "codemirror_mode": "tablegen",
-        "mimetype": "text/x-tablegen",
-        "file_extension": ".td",
-        "pygments_lexer": "text"
-    }
-}

diff  --git a/llvm/utils/TableGen/jupyter/tablegen_kernel/install.py b/llvm/utils/TableGen/jupyter/tablegen_kernel/install.py
index 075c8abfdf6645..22f6b46a133e4c 100644
--- a/llvm/utils/TableGen/jupyter/tablegen_kernel/install.py
+++ b/llvm/utils/TableGen/jupyter/tablegen_kernel/install.py
@@ -3,17 +3,39 @@
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
 import os
+import json
+import sys
 import argparse
+from tempfile import TemporaryDirectory
 from jupyter_client.kernelspec import KernelSpecManager
 
 
 def install_my_kernel_spec(user=True, prefix=None):
     """Install the kernel spec for user in given prefix."""
     print("Installing llvm-tblgen IPython kernel spec")
-    pkgroot = os.path.dirname(__file__)
-    KernelSpecManager().install_kernel_spec(
-        os.path.join(pkgroot, "assets"), "tablegen", user=user, prefix=prefix
-    )
+
+    kernel_json = {
+        "argv": [
+            sys.executable, "-m", "tablegen_kernel", "-f", "{connection_file}"
+        ],
+        "display_name": "LLVM TableGen",
+        "language": "tablegen",
+        "language_info": {
+            "name": "tablegen",
+            "codemirror_mode": "tablegen",
+            "mimetype": "text/x-tablegen",
+            "file_extension": ".td",
+            "pygments_lexer": "text"
+        }
+    }
+
+    with TemporaryDirectory() as tmpdir:
+      json_path = os.path.join(tmpdir, "kernel.json")
+      with open(json_path, 'w') as json_file:
+        json.dump(kernel_json, json_file)
+      KernelSpecManager().install_kernel_spec(
+          tmpdir, "tablegen", user=user, prefix=prefix
+      )
 
 
 def _is_root():


        


More information about the llvm-commits mailing list