Struggling with the below error no module named openai

My startup bot called Chattie was working absolutely fine locally.

I am now trying to deploy on streamlit community cloud and stuck with the below error.

ModuleNotFoundError: No module named ‘openai’

Please note : I have a requirements.txt file on Github with the exact version of openai and streamlit.

Link to my github repository
https://github.com/Sudeepta29/Chattie

Below is the exact error : copy pasted from the log

ModuleNotFoundError: This app has encountered an error. The original error message is redacted to prevent data leaks. Full error details have been recorded in the logs (if you’re on Streamlit Cloud, click on ‘Manage app’ in the lower right of your app).

Traceback:

File "/home/adminuser/venv/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/exec_code.py", line 88, in exec_func_with_error_handling
    result = func()
             ^^^^^^File "/home/adminuser/venv/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 579, in code_to_exec
    exec(code, module.__dict__)File "/mount/src/chattie/app.py", line 3, in <module>
    from chattie_chat import chat_with_chattie, extract_text_from_pdfFile "/mount/src/chattie/chattie_chat.py", line 1, in <module>
    from openai import OpenAI

Hi @sudeeptasahu29, welcome to the forum!

I can’t see your repo, because it’s apparently private, but it looks like you need to add a requirements.txt file to tell streamlit cloud what packages to install (in this case openai) – see App dependencies for your Community Cloud app - Streamlit Docs for more details.

hi, I do have a requirements.txt file. Copy pasting the contents below. I am keeping my repository private because literally 2 mins back, I made it public and my API key was disabled by OpenAI!

streamlit==1.38.0
pdfplumber
openai==1.48.0

Don’t commit your keys. See secrets management.

Anyway, you can just delete the key and not care about it until you sort out the import from openai import OpenAI.

1 Like

from openai import OpenAI, seems to work absolutely fine when running it locally. but while deploying on cloud, its causing an issue. what’s the right command?

hi @blackary - i have made the repo public. Is there anyway you could check and give inputs on what needs to change for the deployment to work?
I really want this app to work :frowning:

The requirements file must be requirements.txt.

@Goyo - the file is requirements.txt

It is Requirements.txt. R and r are not the same character.

@Goyo - thx. that worked. but now I am stuck with the openai API key. When I was running the code locally, i used something called “client” to set the api key in, and then called client_instance to make the responses work. Now making edits to the code , to make it run on cloud- is a bit confusing. any suggestions there please? In advanced settings, I added the openai API key. and now how do i use it using st.secrets in the code?

It is explained here:

Locally you can put the key in your secrets.toml file. In Streamlit Cloud you put your secrets in the advanced settings dialog instead:

Hey @sudeeptasahu29 you can access your secrets in the following way
In the streamlit app settings save your key like this
[“open_ai_key”]
key = “your_api_key”

Within your code, access the secrets

open_ai_key = st.secrets["open_ai_key"]["key"]

client = OpenAI(api_key=open_ai_key)
1 Like

@Goyo thanks! My app is up and working!

1 Like

@joyansbhathena !! you were a life saver! my app is deployed and working!!! thank you!!! :hugs:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.