Search code examples
pythonflaskopenai-api

OpenAI API error: "ModuleNotFoundError: No module named 'openai.error'"


I am having an issue installing the openai.error package in Python. This is what I am trying to install:

from flask import Flask, render_template, request, jsonify
from flask_cors import CORS
import openai
from openai.error import OpenAIError
import re
import os

After installing the OpenAI package, I thought the openai.error package would install automatically. However, it gives me this error:

Traceback (most recent call last):
  File "c:\Users\User\Downloads\Examiner.Production\IELTS_Examiner_Private\app.py", line 4, in <module>
    from openai.error import OpenAIError
ModuleNotFoundError: No module named 'openai.error'

Solution

  • You imported the OpenAIError class from the openai incorrectly.

    Change this...

    from openai.error import OpenAIError
    

    ...to this.

    from openai import OpenAIError