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

使用OpenAI API的Python脚本运行报错:找不到_openai_scripts.py文件

解决Mac上OpenAI Python库运行报错问题

问题现象

运行调用OpenAI API的Python脚本时,终端抛出错误:

/Library/Developer/CommandLineTools/usr/bin/python3: can't open file '/Users/me/Library/Python/3.9/lib/python/site-packages/openai/_openai_scripts.py': [Errno 2] No such file or directory

确认目标目录下无_openai_scripts.py文件,执行openai --help时也出现相同错误。

环境信息

  • 脚本运行命令:python3 chatgpt-analyze.py
  • pip3 show openai输出:
Name: openai
Version: 1.12.0
Summary: The official Python library for the openai API
Home-page:
Author:
Author-email: OpenAI <support@openai.com>
License:
Location: /Users/me/Library/Python/3.9/lib/python/site-packages
Requires: anyio, distro, httpx, pydantic, sniffio, tqdm, typing-extensions
Required-by:

解决方案

1. 修复OpenAI CLI脚本缺失问题

该错误是由于OpenAI库安装不完整,CLI脚本文件丢失导致的,重新安装即可修复:

  • 卸载当前OpenAI库:
    pip3 uninstall -y openai
    
  • 重新安装指定版本(或最新稳定版):
    pip3 install openai==1.12.0
    # 或升级到最新版:pip3 install --upgrade openai
    
  • 验证修复:执行openai --help,若正常输出帮助信息则修复成功。

2. 修正API调用代码错误

你的代码存在接口混用问题:openai.Completion.create是旧版文本补全接口(仅支持prompt参数),而gpt-3.5-turbo模型必须使用对话补全接口openai.ChatCompletion.create,修正后的代码如下:

# Step 2: Send HTML to ChatGPT API
api_key = "..."
prompt = f"HTML source code:\n\n{html_content}\n\nExtract the following information:\n1. Church Name\n2. Pastor Name\n3. Pastor Email"
response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": prompt},
    ],
    api_key=api_key
)

chat_output = response.choices[0].message["content"].strip()

3. 确保Python环境一致性

避免全局环境依赖冲突,建议确认python3与pip3指向的版本一致:

  • 检查版本对应关系:
    which python3
    python3 --version
    which pip3
    pip3 --version
    
  • 若版本不一致,使用虚拟环境隔离依赖:
    # 创建Python3.9虚拟环境
    python3.9 -m venv openai-env
    # 激活虚拟环境
    source openai-env/bin/activate
    # 在虚拟环境中安装OpenAI库
    pip install openai==1.12.0
    # 运行脚本
    python chatgpt-analyze.py
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 13:07:33