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

在Azure Automation Runbook运行OpenAI Python脚本遇distro包安装错误

Azure Automation Runbook调用OpenAI GPT的问题解决及替代方案

一、解决distro包版本冲突问题

1. 清理并重装指定版本的distro包

  • 登录Azure门户,进入目标自动化账户的「模块」页面
  • 搜索distro,删除所有已安装的版本(包括1.7.0、1.8.0)
  • 选择「从PyPI安装」,输入distro==1.8.0并执行安装;或直接上传distro-1.8.0-py3-none-any.whl(该包是纯Python实现,不需要win-amd64版本,Azure Automation的Windows环境可直接使用)

2. 批量打包依赖避免版本不一致

  • 在本地Python 3.10虚拟环境中安装所需依赖(仅保留openai、distro等必要包)
  • 执行pip freeze > requirements.txt导出依赖清单
  • 执行pip wheel -w ./wheels -r requirements.txt生成所有依赖的whl包
  • 将所有生成的whl包批量上传到自动化账户的「模块」中,确保依赖版本与本地一致

3. 脚本内可控安装(优化临时方案)

如果上述方法仍无法解决,可在脚本开头先清理冲突版本再安装指定版本,比直接安装更稳定:

import subprocess
# 强制卸载所有distro版本
subprocess.run(["pip", "uninstall", "-y", "distro"], capture_output=True)
# 安装指定稳定版本
subprocess.run(["pip", "install", "distro==1.8.0"], capture_output=True)
# 后续正常导入和使用
import distro
import openai

二、不依赖openai官方包的GPT调用方法

直接调用OpenAI REST API,仅需依赖requests包(该包可通过自动化账户模块直接安装,无额外冲突),示例代码:

import requests
import json

def get_gpt_response(api_key, prompt):
    url = "https://api.openai.com/v1/chat/completions"
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {api_key}"
    }
    payload = {
        "model": "gpt-3.5-turbo",
        "messages": [{"role": "user", "content": prompt}]
    }
    resp = requests.post(url, headers=headers, json=payload)
    if resp.status_code == 200:
        return resp.json()["choices"][0]["message"]["content"].strip()
    else:
        raise Exception(f"API请求失败: {resp.text}")

# 调用示例
api_key = "你的OpenAI API密钥"
prompt = "请解释Azure Automation Runbook的核心用途"
try:
    response = get_gpt_response(api_key, prompt)
    print(response)
except Exception as e:
    print(str(e))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 19:37:25