Import openai doesn't work in Python 3.12

I have main.py that contains just:
import openai

This import causes an error:
openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

Someone may say that I just need to set OPENAI_API_KEY environment variable, but I want to be able to pass the api_key directly into openai.OpenAI().
I think, it’s definitely a bug, because the user should be able to do ‘import openai’ without setting up OPENAI_API_KEY environment variable.

openai version is 1.12.0
python version is 3.12.2

I tried to switch to python 3.11 and everything works fine there, so I’m going to use 3.11 for now.

Please, let me know if there is a solution for using openai 1.12.0 with python 3.12.2.
And I’m not sure that this is the right place to report such issue.

What IDE or environment are you using?

See if bringing your answer over here helps…

Was this problem ever resolved? I am having a similar issue. I keep receiving the error that says I’ve ran out of time or chances to call the api… I waited a couple of days and I still get the same error.

In case you are new to Python and haven’t learned this by experience yet…

Python 3.12 is out now–but should you switch to it immediately? And if you shouldn’t upgrade just yet, when should you?

Immediately after the release in October 2023, you probably didn’t want to upgrade just yet. But from December 2023 and onwards, and certainly now, upgrading is definitely worth trying, though it may not succeed.

Although, most of the latest python releases are duly stable. It is best to avoid them for two main reasons:

There might be a small bug hiding somewhere in the release which can leave developers scratching their heads when the application is the near go-live.
Some python packages may not be compatible with latest releases.

I would even say: validate code you create against Python 3.8.x - the last version to support Windows Server 2008 R2 and Windows 7 code base, which was widely deployed while later Windows 8.x base was avoided by all. (An OS that doesn’t have a remote access rootkit with a reboot-and-destroy button.)

Looks like it’s PyCharm issue, I tried the same setup in Visual Studio Code and everything works fine. @_j thank you for pointing this out.