Cannot import open Ai

Im getting this error:
ImportError: cannot import name ‘OpenAI’ from ‘openai’

When running this code snippet:
import openai
import re
import httpx
import os
from dotenv import load_dotenv

_ = load_dotenv()
from openai import OpenAI

I am running this locally on my computer, using Jupyter Notebook. Please help what I can do. I have tried uninstalling and reinstalling but no success.

1 Like

@Rakshat_Ranjan
It seems you dont have openai library installed. If that is the case, you need to install it using the command pip install openai
Keep learning

1 Like

Hi Carlos, thanks for the reply.

As mentioned above, I have it installed in my system, but still I get the error.
Wanted to know what steps I can take.
Im running the code in Jupyter Notebook locally on my PC.

1 Like

I use VS code instead of Jupyter but, in the same way, you can create an environment with all necessary libraries, and load it into Jupyter. You can get more details here

1 Like

Hi Carlos,

Thanks for the reply.
I tried the method given by you. I set up a virtual env. Activated it. Installed openai. Ran Jupyter notebook. Still I’m getting the same error.

1 Like

Hi,

I think I have solved it for my system.
The change I made was using prefix “python -m” before installation commands, as explained here.

I uninstalled and reinstalled using
python3 -m pip install openai==1.23.6

This worked, instead of using just pip install.

Thanks.

2 Likes