Memory-Importer is an experimental tool demonstrates that how to import python libraries from MEMORY under PEP 451
| CI | status |
|---|---|
| Linux/macOS Travis | |
| MSVC 2019 | |
| pip builds | |
cibuildwheel |
pip install memory-importer
CMake for building, and pybind11 and physfs.py
memory-importer implement a class called PhysfsImporter follow the finder and loader protocol which are proposed by PEP-451.
After append PhysfsImporter into sys.meta_path, You can import any pure python code from an archive stored in memory( or dicks).
PhysfsImporter use
physfs.pyto easily visit entry in achieve.
Case 1: import from an archive stored in disk.
import sys
from memory_importer import PhysfsImporter
# init importer and mount tests/assets/py.zip into memory
i = PhysfsImporter("tests/assets/py.zip")
# append it into sys.meta_path to notice python use it to import library
sys.meta_path.insert(0, i)
# test import and code
import single
assert str(single) == "<module 'single' (physfs://single.py)>"
assert single.flag() == "single"Case 2: import from archive stored in memory.
import sys
from memory_importer import PhysfsImporter, physfs
# init physfs
physfs.init()
# load archive into memory
with open("tests/assets/py.zip", mode="rb") as fh:
archive = fh.read()
# mount archive into physfs
physfs.mount_memory(archive, "py.zip", "/")
# init importer
i = PhysfsImporter()
# append it into sys.meta_path to notice python use it to import library
sys.meta_path.insert(0, i)
# test import and code
import single
assert str(single) == "<module 'single' (physfs://single.py)>"
assert single.flag() == "single"- Provide a better way to demonstrate how PEP-451 work.
- You can embed your pure python code with your cpp python library.
- Waiting for you to discover...