AzureML Notebook运行Qwen2.5-VL时urllib3相关RuntimeError修复方案
问题
在Azure机器学习Notebooks的Python 3.10 AzureML内核计算实例环境中,运行Qwen2.5-VL模型的图像标注代码时,出现RuntimeError,提示无法从urllib3.util.ssl_导入DEFAULT_CIPHERS。已知问题由urllib3版本过时导致,但无法升级或降级azureml_py38环境以避免兼容性问题,求不破坏环境的解决办法。
运行代码
from transformers import Qwen2_5_VLForConditionalGeneration, AutoTokenizer, AutoProcessor from qwen_vl_utils import process_vision_info import torch model = Qwen2_5_VLForConditionalGeneration.from_pretrained( "Qwen/Qwen2.5-VL-7B-Instruct", torch_dtype="auto", device_map="auto" ) processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-7B-Instruct") messages = [ { "role": "user", "content": [ {"type": "image", "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"}, {"type": "text", "text": "Describe this image."}, ], } ] text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) image_inputs, video_inputs = process_vision_info(messages) inputs = processor(text=[text], images=image_inputs, videos=video_inputs, padding=True, return_tensors="pt") inputs = inputs.to("cuda") generated_ids = model.generate(**inputs, max_new_tokens=128) generated_ids_trimmed = [out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)] output_text = processor.batch_decode(generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False) print(output_text)
报错信息
RuntimeError: Failed to import transformers.models.auto.tokenization_auto because of the following error: Failed to import transformers.generation.utils because of the following error: cannot import name 'DEFAULT_CIPHERS' from 'urllib3.util.ssl_' (/anaconda/envs/azureml_py38/lib/python3.10/site-packages/urllib3/util/ssl_.py)
不破坏环境的解决办法
以下几种方法无需修改azureml_py38基础环境,可直接在当前会话生效:
方法1:临时注入缺失变量
在导入transformers之前,手动给urllib3.util.ssl_模块添加DEFAULT_CIPHERS变量,值取新版urllib3的默认配置。在代码最开头插入:
import urllib3.util.ssl_ # 注入新版urllib3默认的DEFAULT_CIPHERS值 urllib3.util.ssl_.DEFAULT_CIPHERS = "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384"
之后执行原代码即可。
方法2:用户级安装新版urllib3
将新版urllib3安装到用户专属站点目录,Python会优先加载该目录下的包,不会影响系统环境。执行命令:
pip install --user urllib3==2.0.7
安装完成后重启Notebook内核,再运行原代码。
方法3:临时修改模块搜索路径
- 下载新版urllib3源码包并解压到当前Notebook目录(例如命名为
urllib3_new) - 在代码开头添加路径优先加载逻辑:
import sys sys.path.insert(0, "./urllib3_new")
之后再导入transformers等模块即可。
内容的提问来源于stack exchange,提问作者Moon
相关产品推荐
相关产品推荐

