使用pyinstaller对gradio和chromadb进行打包

解决gradio和chromadb的打包问题

背景

python项目里包含了gradio和chromadb模块,使用pyinstaller后总有模块找不到,这里分享一个办法一招解决。

问题

gradio和gradio_client模块

gradio在被打进exe后执行报错:

bash 复制代码
No such file or directory: gradio_client\types.json

这个主要原因是gradio和gradio_client相关模块没有被打进去。

还有一类报错:

bash 复制代码
[Errno 2] No such file or directory: gradio\blocks_events.pyc

这是因为在执行时默认去查找pyc文件,找不到报错。

chromadb模块

chromadb在被打进exe后执行报错:

bash 复制代码
ModuleNotFoundError: No module named'chromadb.telemetry.product.posthog'

这主要是因为chromadb的相关子模块没有被包进去。

解决

针对这两类问题,解决方案分两步:

1.输出.spec文件

bash 复制代码
pyinstaller --collect-data=gradio_client --collect-data=gradio --collect-data=chromadb -F rag_demo.py

2.在生成的rag_demo.spec文件中

Analysis部分添加

bash 复制代码
module_collection_mode={ 'gradio': 'py',}

因为我们希望 gradio 包的 .py 文件以某种特定方式被收集,比如直接将 .py 文件保留在打包后的文件结构中,而不是被编译成 .pyc 文件或者被压缩进 PYZ 文件里。

相关推荐
阿耶同学6 分钟前
手把手教你用 LangGraph 搭建三层嵌套 Agent 架构
python·程序员
花酒锄作田16 小时前
Pydantic校验配置文件
python
hboot17 小时前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi1 天前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi1 天前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab
曲幽1 天前
你的REST接口还在“过度投喂”数据吗?——FastAPI + GraphQL实战避坑指南
python·fastapi·web·graphql·route·cors·rest·strawberry
用户8358086187911 天前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python
Warson_L2 天前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅2 天前
海天线算法的前世今生
python·计算机视觉
韩师傅2 天前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉