Partially initialized module ‘openai’ has no attribute ‘Image’

I tried every thing mentioned above but I still get error
File “C:\Users\DELL\OneDrive\Documents\leet_python\openai.py”, line 5, in
response = openai.Image.create(

AttributeError: partially initialized module ‘openai’ has no attribute ‘Image’ (most likely due to a circular import)
I am using openai version 0.26.5 and python version 3.9.7
Complete program is
import openai
PROMPT = “An eco-friendly computer from the 90s in the style of vaporwave”
openai.api_key = “■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■45rVGzyG”
print(openai.api_key)
response = openai.Image().create(
prompt=PROMPT,
n=1,
size=“256x256”,
)
print(response[“data”][0][“url”])

Hi @shyam_limaye

Your code has an error in this line: openai.Image().create(
did you spot it?

It’s Image()

import openai
PROMPT = “An eco-friendly computer from the 90s in the style of vaporwave”
openai.api_key = “■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■45rVGzyG”
print(openai.api_key)
response = openai.Image().create(
prompt=PROMPT,
n=1,
size=“256x256”,
)
print(response[“data”][0][“url”])

Please refer to docs next time and use boilerplate code.

Also please use proper markdown to share code.

3 Likes

After clicking docs, I got following code.

import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.Image.create(
  prompt="A cute baby sea otter",
  n=2,
  size="1024x1024"
)

I suppose this is official website. Please clarify whether bracket is required.
I tried putting a bracket. It gave the same result.
Please suggest a solution.
Also, please explain what is proper markdown to share code.

Hi @shyam_limaye

What @sps meant to say, I think, was when you post code and data samples, etc, you should always format your code using Markdown, normally using triple backticks, like this:

```
# your code  goes here
# because formatting code makes is so much more easier to read
```

Your code looks good, BTW.

Maybe you can print out (for yourself) your API key so you can make sure you have your ENV variable set up correctly and also assign your image API call to the response variable and print out the entire response back from the API?

When you print the full response, you can see any error message from the API.

Hope this helps.

1 Like

Change the name of your file from “openai.py” to something else. This is causing the circular import.

Please use the code that you found on docs.

@ruby_coder has a great explanation in the replies. Using proper markdown improves readability.

1 Like

The code was crashing due to my own stupidity. I initially created a file called openai.py. Later I realized that it clashes with the library module openai. So I saved it under another name. But did not remove the original. This was the cause of circular reference.
The code started working after I removed the offending file.
Thanks for help.
The code works with bracket and also without bracket. I wonder how?
I am still not clear about markdown. Please explain.

2 Likes

Here ya go @shyam_limaye

1 Like

Markdown Cheat Sheet | Markdown Guide

Thanks for sending the markdown guide.

1 Like

Did you type the code from memory, or where do the curly quotes come from? :sweat_smile: