使用Python wget下载HuggingFace模型权重遇OSError错误求助
错误原因与解决方法
错误含义
OSError: [Errno 97] Address family not supported by protocol 表示Python尝试使用当前系统或网络环境不支持的网络地址族(通常是IPv6)建立连接,导致无法访问HuggingFace的服务器。
解决办法
1. 强制Python使用IPv4连接
在你的wget代码前添加以下代码,强制socket模块只用IPv4:
import socket # 强制使用IPv4地址族 socket.setdefaulttimeout(15) original_socket = socket.socket def force_ipv4_socket(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0, fileno=None): return original_socket(family, type, proto, fileno) socket.socket = force_ipv4_socket # 原wget代码 import wget wget.download("https://huggingface.co/h94/IP-Adapter/resolve/main/models/image_encoder/pytorch_model.bin")
2. 使用HuggingFace官方下载工具(推荐)
huggingface-hub是官方提供的模型下载库,更稳定且支持断点续传,能避免这类网络协议问题:
- 先安装库:
pip install huggingface-hub
- 替换为以下下载代码:
from huggingface_hub import hf_hub_download # 下载指定文件到本地目录 hf_hub_download( repo_id="h94/IP-Adapter", filename="models/image_encoder/pytorch_model.bin", local_dir="./" # 可修改为你想要保存的路径 )
3. 检查系统网络配置
- 关闭不必要的IPv6:如果你的网络环境不支持IPv6,可在系统设置中关闭IPv6协议。
- 排查代理/防火墙:确认防火墙或代理没有阻止Python程序的网络请求。
内容的提问来源于stack exchange,提问作者Flicic Suo
相关产品推荐
相关产品推荐

