- Create an isolated Python environment
- Install required packages
- Store your API key safely
- Write and run a simple Python script using the OpenAI client
1. (Optional) Create and Activate a Virtual Environment
Isolating dependencies prevents conflicts across projects.Using a virtual environment is optional but recommended for dependency management.
2. Install the OpenAI Python Package
With your environment active, install the official OpenAI library:3. Configure Your API Key as an Environment Variable
Storing credentials in environment variables keeps them out of your codebase. Use the command for your OS:| Platform | Command |
|---|---|
| macOS/Linux | export OPENAI_API_KEY="your_api_key_here" |
| Windows (PowerShell) | setx OPENAI_API_KEY "your_api_key_here" |
OpenAI Platform → Settings → API Keys → Create new secret key.
Never commit your API key—or any secrets—to version control. Consider adding
.env or environment-specific files to your .gitignore.4. Write Your Python Script
Create a file namedexample.py: