Pyodide的导入钩子
项目描述
pyodide-importer
Pyodide + 外部文件导入支持
安装
Pyodide内部
import micropip
await micropip.install("pyodide-importer")
基本用法
from pyodide_importer import register_hook
register_hook("<Base URL where python scripts are in>")
import external_module
示例
import micropip
await micropip.install("pyodide-importer")
from pyodide_importer import register_hook
register_hook("https://raw.githubusercontent.com/ryanking13/pyodide-importer/main/test_modules/")
# https://raw.githubusercontent.com/ryanking13/pyodide-importer/main/test_modules/file_module.py
import file_module
file_module.hello()
# https://raw.githubusercontent.com/ryanking13/pyodide-importer/main/test_modules/regular_module/__init__.py
import regular_module
regular_module.hello()
高级用法
白名单模块导入
白名单模块将防止在查找不存在模块时生成冗余的HTTP请求。
register_hook(
"<Base URL where python scripts are in>",
modules=["module1", "module2"]
)
更改模块下载的路径
download_path
选项更改了模块将在虚拟文件系统中下载的路径,默认设置为当前工作目录。
register_hook(
"<Base URL where python scripts are in>",
download_path="/path/to/be/downloaded",
)