python-dotenv loads key-value settings from a .env file into environment variables, keeping API keys out of scripts and plain-text sharing. section 2.4.3
Use the library through a short setup workflow:
- Create a .env file in the project folder and place the API key there. section 2.4.3
- Install python-dotenv from the terminal. section 2.4.3
- Call load_dotenv(), then retrieve the key with os.getenv("OPENAI_API_KEY"). section 2.4.3
from dotenv import load_dotenv
import os
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
Shows the minimal Python integration for loading .env values and assigning the API key. section 2.4.3
