Kaggle API Client
The `kagglesdk` is the official Python client library for interacting with Kaggle's external-facing APIs, allowing users to programmatically access competitions, datasets, kernels, and more. It is currently at version 0.1.18 and receives frequent updates.
Warnings
- gotcha The PyPI package name is `kagglesdk`, but it is imported as the `kaggle` module (e.g., `import kaggle`). Attempting to import directly from `kagglesdk` (e.g., `from kagglesdk import api`) will result in an `ImportError` or unexpected behavior.
- gotcha Authentication is typically managed by `kaggle.api.authenticate()`, which looks for a `kaggle.json` file in `~/.kaggle/` or the `KAGGLE_USERNAME` and `KAGGLE_KEY` environment variables. If credentials are not found, an `ApiException` will be raised.
- gotcha When downloading files (e.g., `dataset_download_files`, `competition_download_files`), the `path` argument specifies the destination directory. If a relative path is provided, it's relative to the current working directory. Files are often downloaded as ZIP archives, and `unzip=True` is commonly used.
- gotcha The Kaggle API client does not automatically handle rate limiting. Frequent or rapid requests, especially in loops or automated scripts, can lead to your IP being temporarily blocked by Kaggle's servers.
- gotcha API methods typically return custom objects (e.g., `Competition`, `Dataset`) or lists of these objects, not raw Python dictionaries. Access data using dot notation (e.g., `competition.ref`, `competition.title`).
Install
-
pip install kagglesdk
Imports
- kaggle
import kaggle
Quickstart
import os
import kaggle
# --- Kaggle API Authentication ---
# The client expects credentials in one of two ways:
# 1. A `kaggle.json` file in `~/.kaggle/` (recommended for local development)
# 2. Environment variables: `KAGGLE_USERNAME` and `KAGGLE_KEY`
# For example, in your shell:
# export KAGGLE_USERNAME="your_username"
# export KAGGLE_KEY="your_api_key"
# Initialize and authenticate the API client
# This call implicitly uses the `kaggle.json` file or environment variables.
kaggle.api.authenticate()
print("Successfully authenticated to Kaggle API.")
# --- Example Usage: List active competitions ---
print("\nActive Competitions:")
try:
# competition_list returns a generator-like object
competitions = kaggle.api.competition_list(category='active', sort_by='recentlyStarted')
for competition in competitions:
print(f"- {competition.ref}: {competition.title}")
except Exception as e:
print(f"Error listing competitions: {e}")
# --- Example Usage: Download a dataset (uncomment to run) ---
# dataset_name = 'titanic'
# download_path = './data'
# os.makedirs(download_path, exist_ok=True)
# print(f"\nDownloading dataset '{dataset_name}' to '{download_path}'...")
# try:
# kaggle.api.dataset_download_files(dataset_name, path=download_path, unzip=True)
# print(f"Dataset '{dataset_name}' downloaded and unzipped successfully.")
# except Exception as e:
# print(f"Error downloading dataset '{dataset_name}': {e}")