Package: kagglehub
Kaggle Hub Client LibraryInstallationInstall thekagglehubpackage with pip:pip install kagglehubUsageAuthenticateAuthenticating isonlyneeded to access public resources requiring user consent or private resources.First, you will need a Kaggle account. You can sign uphere.After login in, you can download your Kaggle API credentials athttps://www.kaggle.com/settingsby clicking on the "Create New Token" button under the "API" section.You have 3 different options to authenticate.Option 1: Calling kagglehub.login()This will prompt you to enter your username and token:importkagglehubkagglehub.login()Option 2: Read credentials from environment variablesYou can also choose to export your Kaggle username and token to the environment:exportKAGGLE_USERNAME=datadinosaurexportKAGGLE_KEY=xxxxxxxxxxxxxxOption 3: Read credentials fromkaggle.jsonStore yourkaggle.jsoncredentials file at~/.kaggle/kaggle.json.Alternatively, you can set theKAGGLE_CONFIG_DIRenvironment variable to change this location to$KAGGLE_CONFIG_DIR/kaggle.json.Note for Windows users: The default directory is%HOMEPATH%/kaggle.json.Download ModelThe following examples download theanswer-equivalence-bemvariation of this Kaggle model:https://www.kaggle.com/models/google/bert/frameworks/tensorFlow2/variations/answer-equivalence-bemimportkagglehub# Download the latest version.kagglehub.model_download('google/bert/tensorFlow2/answer-equivalence-bem')# Download a specific version.kagglehub.model_download('google/bert/tensorFlow2/answer-equivalence-bem/1')# Download a single file.kagglehub.model_download('google/bert/tensorFlow2/answer-equivalence-bem',path='variables/variables.index')# Download a model or file, even if previously downloaded to cache. Only available outside Kaggle notebooks.kagglehub.model_download('google/bert/tensorFlow2/answer-equivalence-bem',force_download=True)Upload ModelUploads a new variation (or a new variation's version if it already exists).importkagglehub# For example, to upload a new variation to this model:# - https://www.kaggle.com/models/google/bert/frameworks/tensorFlow2/variations/answer-equivalence-bem## You would use the following handle: `google/bert/tensorFlow2/answer-equivalence-bem`handle='<KAGGLE_USERNAME>/<MODEL>/<FRAMEWORK>/<VARIATION>'local_model_dir='path/to/local/model/dir'kagglehub.model_upload(handle,local_model_dir)# You can also specify some version notes (optional)kagglehub.model_upload(handle,local_model_dir,version_notes='improved accuracy')# You can also specify a license (optional)kagglehub.model_upload(handle,local_model_dir,license_name='Apache 2.0')DevelopmentPrequisitesWe usehatchto manage this project.Follow theseinstructionsto install it.Tests# Run all testshatchruntest# Run a single test filehatchruntesttests/test_<SOME_FILE>.pyIntegration TestsTo run integration tests on your local machine, you need to set up your Kaggle API credentials. You can do this in one of these two ways described in the earlier sections of this document. Refer to the sections:Using environment variablesUsing credentials fileAfter setting up your credentials by any of these methods, you can run the integration tests as follows:# Run all testshatchrunintegration-testRunkagglehubfrom source# Download a model & print the pathhatchrunpython-c"import kagglehub; print('path: ', kagglehub.model_download('google/bert/tensorFlow2/answer-equivalence-bem'))"Lint / Format# Lint checkhatchrunlint:style
hatchrunlint:typing
hatchrunlint:all# for both# Formathatchrunlint:fmtCoverage reporthatchcovBuildhatchbuildRunninghatchcommands inside DockerThis is useful to run in a consistent environment and easily switch between Python versions.The following shows how to runhatch run lint:allbut this also works for any other hatch commands:# Use default Python version
./docker-hatch run lint:all

# Use specific Python version (Must be a valid tag from: https://hub.docker.com/_/python)
./docker-hatch -v 3.9 run lint:all