Package: paker
PakerPaker is module for importing Python packages/modules from dictionaries and JSON formatted documents.
It was inspired byhttpimporter.Important:Since v0.6.0pakersupports importing.pydand.dllmodules directly from memory.
This was achieved by using_memimporterfrompy2exeproject.
Importing.sofiles on Linux still requires writing them to disk.InstallationFrom PyPIpipinstallpaker-UFrom sourcegitclonehttps://github.com/desty2k/paker.gitcdpaker
pipinstall.UsageLet's usepakerto dumpprint_libmodule with following directory structure:print_lib/
├── __init__.py
├── capitalize.py
└── decapitalize.pycapitalized.pycontainsPrintfunction anddecapitalized.pycontainspRINTfunction.__init__.pyfile is empty.Dumping to JSONTo dump the library to a file usepaker dumpcommand:pakerdumpprint_lib--indent4Paker will create new fileprint_lib.jsonin the current directory.{"print_lib":{"type":"package","extension":"py","code":"","modules":{"capitalize":{"type":"module","extension":"py","code":"def Print(text: str, *args, **kwargs):\n    print(text.capitalize(), *args, **kwargs)\n"},"decapitalize":{"type":"module","extension":"py","code":"def pRINT(text: str, *args, **kwargs):\n    print(text[0].lower() + text[1:].upper(), *args, **kwargs)\n"}}}}Listing modulesTo list all modules in the JSON usepaker listcommand:pakerlistprint_lib.jsonOutput:P print_lib
M print_lib.capitalize
M print_lib.decapitalizeRecreating package from JSONTo recreate the package from the JSON usepaker loadcommand.pakerloadprint_lib.jsonprint_libpackage will be created in the current directory. To set custom output path use--outputoption.Importing from.jsonNote:Importing from original source code has higher priority than importing from JSON.
Remember to remove module from disk before importing the library withpaker.importpakerimportlogginglogging.basicConfig(level=logging.NOTSET)if__name__=='__main__':withopen("print_lib.json","r")asf:withpaker.load(f)asloader:# print_lib will be available only in this contextfromprint_lib.capitalizeimportPrintfromprint_lib.decapitalizeimportpRINTPrint("hello world!")pRINT("hello world!")This script produces the following output:DEBUG:jsonimporter:searching for print_lib
INFO:jsonimporter:print_lib has been imported successfully
DEBUG:jsonimporter:searching for print_lib.capitalize
INFO:jsonimporter:print_lib.capitalize has been imported successfully
DEBUG:jsonimporter:searching for print_lib.decapitalize
INFO:jsonimporter:print_lib.decapitalize has been imported successfully

Hello world!
hELLO WORLD!

INFO:jsonimporter:unloaded all modulesImporting from memoryYou can also import Python modules directly from memory.
Libraries can be loaded not only from Pythondictobjects, but also fromstr,bytesandbytearrayobjects.importpakerimportlogging# paker.loads accepts dict, str, bytes and bytearray objectsPOW={"pow":{"type":"module","extension":"py","code":"pow = lambda x, y: x**y"}}SQR='{"sqr": {"type": "module", "extension": "py", "code": "from pow import pow\\nsqr = lambda x: pow(x, 2)"}}'TRI=b'{"tri": {"type": "module", "extension": "py", "code": "from pow import pow\\ntri = lambda x: pow(x, 3)"}}'logging.basicConfig(level=logging.NOTSET)if__name__=='__main__':# you can use nested loaderswithpaker.loads(POW)aspow_loader:# pow will be available only in this contextwithpaker.loads(SQR)assqr_loader:# sqr will be available only in this contextfromsqrimportsqrassertsqr(2),4assertsqr(5),25print("6**2 is{}".format(sqr(6)))withpaker.loads(TRI)astri_loader:# tri will be available only in this contextfromtriimporttriasserttri(2),8asserttri(5),125print("6**3 is{}".format(tri(6)))print("It works!")This script produces the following output:DEBUG:jsonimporter:searching for sqr
INFO:jsonimporter:sqr has been added successfully
DEBUG:jsonimporter:searching for sqr
DEBUG:jsonimporter:searching for pow
INFO:jsonimporter:pow has been imported successfully
INFO:jsonimporter:sqr has been imported successfully

6**2 is 36

INFO:jsonimporter:sqr has been unloaded successfully
DEBUG:jsonimporter:searching for tri
INFO:jsonimporter:tri has been added successfully
DEBUG:jsonimporter:searching for tri
INFO:jsonimporter:tri has been imported successfully

6**3 is 216

INFO:jsonimporter:tri has been unloaded successfully

It works!

INFO:jsonimporter:unloaded all modulesHow it worksWhen importing modules or packages Python iterates overimportersinsys.meta_pathand callsfind_modulemethod on each object.
If the importer returns itself, it means that the module can be imported andNonemeans that importer did not find searched package.
If any importer has confirmed the ability to import module, Python executes another method on it -load_module.
Paker implements its own importer calledjsonimporter, which instead of searching for modules in directories, looks for them in Python dictionariesTo dump module or package to JSON document, Paker recursively iterates over modules and creates dict with
code and type of each module and submodules if object is package.