Skip to main content
This guide walks through configuring a local Python development environment to experiment with the OpenAI API. The steps work whether you’re using a pre-configured environment (e.g., KodeKloud) or your own workstation. Quick overview of the setup flow:
  1. Install Python (3.10+).
  2. Create and activate a Python virtual environment.
  3. Install required Python packages (openai and jupyter).
  4. Create an OpenAI API key and export it as an environment variable.
  5. Test the API from the command line (curl).
  6. Test the API from a Python Jupyter notebook.
The image outlines six steps for setting up a Python environment to work with OpenAI and Jupyter, including installing Python and PIP, creating a virtual environment, installing necessary modules, obtaining an API key, setting an environment variable, and testing the setup.
For quick reference, here’s a condensed table of common commands used in this guide:

1) Install Python

Download and install Python 3.10 or later from the official site: https://www.python.org/downloads/. Choose the installer that matches your OS (Windows, macOS, Linux). Example usage (can be run in the Python REPL or a script):
Verify the Python installation:
The image shows a webpage from python.org offering the download of Python 3.11.4 for macOS. It includes a section listing active Python releases and their maintenance statuses.

2) pip and creating a virtual environment

Modern Python installers include pip, which you’ll use to install libraries. Always use a virtual environment to isolate dependencies per project. Create and activate a virtual environment (Unix/macOS):
On Windows (PowerShell):
Using a venv prevents global package conflicts and makes dependency management reproducible.

3) Install the OpenAI Python package and Jupyter

With the virtual environment activated, install the required packages:
If the wheels are cached, pip will reuse them and the install will be fast. Tip: consider pinning package versions in a requirements.txt for reproducible installs:
Then install via:

4) Create an OpenAI API key

  1. Sign in to the OpenAI platform: https://platform.openai.com/account/api-keys
  2. Create a new API key and give it a descriptive name (e.g., KodeKloud-dev).
  3. Copy the secret value immediately — you will not be able to view the full secret after you close the dialog.
The image shows the OpenAI platform interface, featuring links to a quickstart tutorial, examples, and sections on building applications and ChatGPT plugins.
The image shows a webpage where a user is creating a new secret API key, with a dialog box for naming the key.
Keep your API key secret. Copy it now because you will not be able to see it again after closing the creation dialog.
Example: export the key for the current shell session (do not paste a real key here — use the key you copied):
For PowerShell (temporary for the session):
To persist the key across sessions:
  • Add the export line to your shell startup file (e.g., ~/.bashrc, ~/.zshrc) on macOS/Linux.
  • Or add the PowerShell line to your PowerShell profile for Windows.
The image shows a webpage displaying API key management on the OpenAI platform, with options to create a new secret key and set the default organization.
Do not commit your API key to version control or paste it into public forums. Use environment variables or a secrets manager for production applications.

5) Test the API from the command line (curl)

Use curl to verify the environment and the OPENAI_API_KEY variable. This example calls the Chat Completions endpoint (gpt-3.5-turbo) and asks a simple timezone question.
A typical (abridged) JSON response:
Understanding the usage block (prompt_tokens, completion_tokens, total_tokens) helps you optimize prompts and control costs.

6) Test from a Jupyter notebook (Python)

Start Jupyter:
Open a new notebook (e.g., “Test”) and run this code in a cell. The code reads the API key from the environment and uses the OpenAI Python client to create a chat completion.
The image shows a Jupyter Notebook interface, with a toolbar and an empty code cell ready for input. The notebook is open in a web browser window titled "Test."
Example returned JSON (abridged):
This confirms:
  • Command-line access with curl is functional.
  • Python access via the OpenAI client works inside Jupyter.
  • The OPENAI_API_KEY environment variable is being used correctly.
Next steps: we will dive deeper into tokenization, prompt design, and cost optimization for production-ready applications.

Watch Video