Commit 34c75091 authored by Romain Casati's avatar Romain Casati
Browse files

Simpler installation of user modules.

parent 6457899d
......@@ -5,6 +5,7 @@ This is the Python part of the Basthon Kernel.
import pyodide
import sys
import importlib
import os
import pydoc
from js import document, window
......@@ -65,7 +66,10 @@ class BasthonNamespace(object):
def __init__(self):
self.execution_count = None
self._namespace = None
self._user_modules_root = "basthon_user_modules"
self._user_modules_root = "/basthon_user_modules"
# we don't insert at position 0 since
# https://github.com/iodide-project/pyodide/issues/737#issuecomment-750858417
sys.path.insert(2, self._user_modules_root)
self._init_basthon_internal()
self.start()
......@@ -245,18 +249,21 @@ class BasthonNamespace(object):
def put_module(self, filename, content):
"""
Put a module (*.py file) on the (emulated) local filesystem
bypassing the Pyodide' single-import-issue by using separated
directories for each module.
bypassing the Pyodide' single-import-issue by invalidate caches
https://github.com/iodide-project/pyodide/issues/737
/!\ Warning: the dependencies loading is done on the JS side by
basthon.js.
"""
_, filename = os.path.split(filename)
dirname = os.path.splitext(filename)[0]
root = os.path.join('/', self._user_modules_root, dirname)
self.put_file(os.path.join(root, filename), content)
sys.path.insert(0, root)
module_path = os.path.join(self._user_modules_root, filename)
self.put_file(module_path, content)
file_finder = sys.path_importer_cache.get(self._user_modules_root)
if file_finder is None:
# can't figure out why this is needed...
importlib.invalidate_caches()
else:
file_finder.invalidate_caches()
def get_file(self, filepath):
"""
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment