We will create a Python module in an in-memory filesystem, import it, and remove its importer.
Use Python 3.10 or newer:
python -m pip install fsspec-pythonCreate import_demo.py:
import fsspec
from fsspec_python import install_importer, uninstall_importer
fs = fsspec.filesystem("memory")
fs.pipe_file("/import-demo/hello_remote.py", b'def greet():\n return "Hello from memory"\n')
print(fs.ls("/import-demo", detail=False))Run python import_demo.py. The listing contains /import-demo/hello_remote.py.
Append:
importer = install_importer(fs, fo="/import-demo")
try:
import hello_remote
print(hello_remote.greet())
finally:
uninstall_importer(importer)Run the script again. Its final line is:
Hello from memory
Append:
import sys
print("hello_remote" in sys.modules)Run the script. The final line is False: removing the importer also removed its
tracked module from Python's module cache.
See How to install and remove importers to use a filesystem URL.