First_agent_template issue app.py

Hi everyone,

I’m new here and currently working on my first agent course. I’m trying to build my first agent, but I’m having some difficulties managing the API key.

I followed the steps outlined in this discussion (ysharma/ChatGPT4 · how to use openai api key?), but I’m still encountering an error.

I put this code at the top

from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
import datetime
import requests
import pytz
import yaml
from tools.final_answer import FinalAnswerTool
from openai import OpenAI
import gradio as gr
import os
api_key = os.getenv(“OPENAI_API_KEY”)
client = OpenAI(api_key=api_key)

==================================================

runtime error

Exit code: 1. Reason: Traceback (most recent call last): File “/home/user/app/app.py”, line 7, in from openai import OpenAI ModuleNotFoundError: No module named ‘openai’

Container logs:

===== Application Startup at 2025-05-27 16:31:22 =====

Traceback (most recent call last):
  File "/home/user/app/app.py", line 7, in <module>
    from openai import OpenAI
ModuleNotFoundError: No module named 'openai'
1 Like

Have you already installed OpenAI?

pip install openai

Let us know if you’re using a Hugging Face Space, Dockerfile, or something else — We can tailor the solution further!

1 Like

Handling Spaces Dependencies gives more information about Space dependencies.

2 Likes

Thanks for your reply.

Currently, I am following the course outline to use the space from Hugging Face.

1 Like

If you have space on your base, add the following line to requirements.txt and it should work. If you don’t haverequirements.txt, just create it.

openai