Dotenv module error

I’m working on making a chatbot with twilio and OpenAI, and here’s my code:

import os
from dotenv import load_dotenv
import openai

load_dotenv()
openai.api_key = os.environ.get(‘OPENAI_KEY’)
completion = openai.Completion()

start_chat_log = ‘’‘Human: Hello, who are you?
AI: What’s up?
‘’’

def ask(question, chat_log=None):
if chat_log is None:
chat_log = start_chat_log
prompt = f’{chat_log}Human: {question}\nAI:’
response = completion.create(
prompt=prompt, engine=“davinci”, stop=[’\nHuman’], temperature=0.9,
top_p=1, frequency_penalty=0, presence_penalty=0.6, best_of=1,
max_tokens=150)
answer = response.choices[0].text.strip()
return answer

def append_interaction_to_chat_log(question, answer, chat_log=None):
if chat_log is None:
chat_log = start_chat_log
return f’{chat_log}Human: {question}\nAI: {answer}\n’

and even though I did pip install python-dotenv in my virtual environment that my project is in, when I try to run my code I’m getting this error:

ModuleNotFoundError: No module named ‘dotenv’

got any ideas for me?

It can’t seem to find that package in the search path. So a couple of things to test (apologies if you already have)-

Is the environment activated when you run the script?
Was it activated when you installed?
How are you running this script?
python name.py ?
via vs code or a similar editor?

A few details like your OS and the output of python -m pip list inside the activated environment would be useful.

The problem so far as I can tell has nothing to do with the code and everything to do with how it is being run. So the commands you used to run things are important (paste, screenshot or similar of the steps you took might be useful).

If you’re posting code to the forums please view- How do I format code in my posts? especially for something like Python where the missing indentation makes it unclear what happens when.

1 Like