You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyInstaller4.4打包报错RuntimeError: No metadata path found for 'greenlet'如何解决

完整报错信息
Traceback (most recent call last):
File "c:\programdata\anaconda3\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\programdata\anaconda3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\ProgramData\Anaconda3\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\__main__.py", line 126, in run
run_build(pyi_config, spec_file, **vars(args))
File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\__main__.py", line 65, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 758, in main
build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 705, in build
exec(code, spec_namespace)
File "C:\Users\28912\Desktop\main.spec", line 19, in <module>
noarchive=False)
File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 254, in __init__
self.__postinit__()
File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\building\datastruct.py", line 159, in __postinit__
self.assemble()
File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 433, in assemble
self.graph.process_post_graph_hooks(self)
File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\depend\analysis.py", line 373, in process_post_graph_hooks
module_hook.post_graph(analysis)
File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\depend\imphook.py", line 451, in post_graph
self._load_hook_module()
File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\depend\imphook.py", line 409, in _load_hook_module
self.hook_module_name, self.hook_filename)
File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\compat.py", line 632, in importlib_load_source
return mod_loader.load_module()
File "<frozen importlib._bootstrap_external>", line 399, in _check_name_wrapper
File "<frozen importlib._bootstrap_external>", line 823, in load_module
File "<frozen importlib._bootstrap_external>", line 682, in load_module
File "<frozen importlib._bootstrap>", line 265, in _load_module_shim
File "<frozen importlib._bootstrap>", line 684, in _load
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\hooks\hook-gevent.py", line 25, in <module>
datas += copy_metadata('gevent', recursive=True)
File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 940, in copy_metadata
dest = _copy_metadata_dest(dist.egg_info, dist.project_name)
File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 975, in _copy_metadata_dest
f"No metadata path found for distribution '{project_name}'.")
RuntimeError: No metadata path found for distribution 'greenlet'.
问题原因
  • 代码中没有主动导入greenlet是因为报错触发自PyInstaller 4.4自带的hook-gevent.py钩子,该钩子会自动扫描环境中的gevent依赖并尝试复制元数据,greenlet是gevent的依赖组件,触发报错的常见场景有两类:
    • Python环境中安装了gevent,但greenlet安装不完整、缺少元信息,Anaconda环境下的裁剪依赖包容易出现该问题
    • 项目完全不需要gevent,PyInstaller误触发了gevent相关钩子
解决方案

方案1:项目无需gevent(推荐)

直接在打包时排除gevent模块即可,无需修改项目代码:

  • 命令行打包时添加排除参数:
pyinstaller --exclude-module=gevent 你的主脚本名称.py
  • 若使用spec文件打包,修改spec文件中Analysis段的excludes参数,添加'gevent'即可:
a = Analysis(
    ['你的主脚本名称.py'],
    excludes=['gevent'],
    # 其余原有参数保持不变
)

方案2:项目需要使用gevent

重新安装完整的greenlet和gevent包,修复缺失的元数据:

# 先卸载原有包
pip uninstall -y greenlet gevent
# 重新安装完整版本
pip install greenlet gevent
# 若使用Anaconda环境,可替换为conda安装
# conda install greenlet gevent

方案3:升级PyInstaller版本

该问题是PyInstaller 4.4版本的已知钩子误触发问题,升级到更高稳定版本即可修复:

# 升级到最新稳定版PyInstaller
pip install --upgrade pyinstaller
# 若需要保留4.x版本特性,可升级到4.x最终稳定版
# pip install pyinstaller==4.10

内容的提问来源于stack exchange,提问作者shiji lao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 16:00:03