Kaggle is a platform that offers machine learning competitions, allows to make submissions, download public datasets, and more. Kaggle has large community of data scientists and machine learning engineers. We can use Kaggle website to manually perform desired actions. However, Kaggle offers API that can be used to automate tasks such download the dataset.
Install Kaggle API
Kaggle provides official API client for Python. It can be installed using
pip package manager from the command line:
pip install kaggle
Get API token
To interact with Kaggle API, we need an API token. Follow these steps to create token:
- Sign in to Kaggle and go to account page
- Scroll down to the API section and click “Create New API Token”.
A file called
kaggle.json will be downloaded. This file contains username and key required for API authentication.
By default, Kaggle API client checks the
KAGGLE_KEY environment variables to authenticate the user with the API. These variables can be populated with values from
If environment variables not set then API client checks
kaggle.json configuration file which can placed at
~/.kaggle/kaggle.json on Linux or Mac, and at
C:\Users\<USERNAME>\.kaggle\kaggle.json on Windows.
Testing Kaggle API
The following code authenticates the user and prints a list of competitions.
import os os.environ['KAGGLE_USERNAME'] = 'YOUR_USERNAME' os.environ['KAGGLE_KEY'] = 'YOUR_KEY' from kaggle.api.kaggle_api_extended import KaggleApi api = KaggleApi() api.authenticate() competitions = api.competitions_list() print(competitions)
Uninstall Kaggle API
If you want to completely remove Kaggle API client, run the following command:
pip uninstall kaggle