使用 pip 安裝kagglehub
套件:
pip install kagglehub
僅當存取需要使用者同意的公共資源或私有資源時才需要進行身份驗證。
首先,您需要一個 Kaggle 帳戶。您可以在這裡註冊。
登入後,您可以透過點擊「API」部分下的「建立新令牌」按鈕,在 https://www.kaggle.com/settings 下載您的 Kaggle API 憑證。
您有 3 種不同的身份驗證選項。
這將提示您輸入使用者名稱和令牌:
import kagglehub
kagglehub . login ()
您也可以選擇將 Kaggle 使用者名稱和令牌匯出到環境中:
export KAGGLE_USERNAME=datadinosaur
export KAGGLE_KEY=xxxxxxxxxxxxxx
kaggle.json
讀取憑證將您的kaggle.json
憑證檔案儲存在~/.kaggle/kaggle.json
。
或者,您可以設定KAGGLE_CONFIG_DIR
環境變數以將此位置變更為$KAGGLE_CONFIG_DIR/kaggle.json
。
Windows 使用者註意:預設目錄是%HOMEPATH%/kaggle.json
。
將您的使用者名稱和金鑰令牌儲存為 Colab 秘密KAGGLE_USERNAME
和KAGGLE_KEY
。
有關在 Colab 和 Colab Enterprise 中新增機密的說明,請參閱本文。
以下範例下載此 Kaggle 模型的answer-equivalence-bem
變體:https://www.kaggle.com/models/google/bert/tensorFlow2/answer-equivalence-bem
import kagglehub
# 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.
kagglehub . model_download ( 'google/bert/tensorFlow2/answer-equivalence-bem' , force_download = True )
上傳新變體(如果已存在,則上傳新變體的版本)。
import kagglehub
# For example, to upload a new variation to this model:
# - https://www.kaggle.com/models/google/bert/tensorFlow2/answer-equivalence-bem
#
# You would use the following handle: `google/bert/tensorFlow2/answer-equivalence-bem`
handle = '///'
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' )
# You can also specify a list of patterns for files/dirs to ignore.
# These patterns are combined with `kagglehub.models.DEFAULT_IGNORE_PATTERNS`
# to determine which files and directories to exclude.
# To ignore entire directories, include a trailing slash (/) in the pattern.
kagglehub . model_upload ( handle , local_model_dir , ignore_patterns = [ "original/" , "*.tmp" ])
以下範例下載Spotify Recommendation
Kaggle 資料集:https://www.kaggle.com/datasets/bricevergnou/spotify-recommendation
import kagglehub
# Download the latest version.
kagglehub . dataset_download ( 'bricevergnou/spotify-recommendation' )
# Download a specific version.
kagglehub . dataset_download ( 'bricevergnou/spotify-recommendation/versions/1' )
# Download a single file
kagglehub . dataset_download ( 'bricevergnou/spotify-recommendation' , path = 'data.csv' )
# Download a dataset or file, even if previously downloaded to cache.
kagglehub . dataset_download ( 'bricevergnou/spotify-recommendation' , force_download = True )
上傳新資料集(如果已存在,則上傳新版本)。
import kagglehub
# For example, to upload a new dataset (or version) at:
# - https://www.kaggle.com/datasets/bricevergnou/spotify-recommendation
#
# You would use the following handle: `bricevergnou/spotify-recommendation`
handle = ' < KAGGLE_USERNAME > / < DATASET >
local_dataset_dir = 'path/to/local/dataset/dir'
# Create a new dataset
kagglehub . dataset_upload ( handle , local_dataset_dir )
# You can then create a new version of this existing dataset and include version notes (optional).
kagglehub . dataset_upload ( handle , local_dataset_dir , version_notes = 'improved data' )
# You can also specify a list of patterns for files/dirs to ignore.
# These patterns are combined with `kagglehub.datasets.DEFAULT_IGNORE_PATTERNS`
# to determine which files and directories to exclude.
# To ignore entire directories, include a trailing slash (/) in the pattern.
kagglehub . dataset_upload ( handle , local_dataset_dir , ignore_patterns = [ "original/" , "*.tmp" ])
以下範例下載Digit Recognizer
Kaggle 競賽:https://www.kaggle.com/competitions/digit-recognizer
import kagglehub
# Download the latest version.
kagglehub . competition_download ( 'digit-recognizer' )
# Download a single file
kagglehub . competition_download ( 'digit-recognizer' , path = 'train.csv' )
# Download a competition or file, even if previously downloaded to cache.
kagglehub . competition_download ( 'digit-recognizer' , force_download = True )
我們使用孵化來管理這個專案。
請按照以下說明進行安裝。
# Run all tests for current Python version.
hatch test
# Run all tests for all Python versions.
hatch test --all
# Run all tests for a specific Python version.
hatch test -py 3.11
# Run a single test file
hatch test tests/test_ < SOME_FILE > .py
要在本機上執行整合測試,您需要設定 Kaggle API 憑證。您可以透過本文檔前面部分中所述的兩種方法之一來執行此操作。請參閱以下部分:
透過任何這些方法設定憑證後,您可以執行整合測試,如下所示:
# Run all tests
hatch test integration_tests
kagglehub
# Download a model & print the path
hatch run python -c " import kagglehub; print('path: ', kagglehub.model_download('google/bert/tensorFlow2/answer-equivalence-bem')) "
# Lint check
hatch run lint:style
hatch run lint:typing
hatch run lint:all # for both
# Format
hatch run lint:fmt
hatch test --cover
hatch build
hatch
命令這對於在一致的環境中運行並在 Python 版本之間輕鬆切換非常有用。
下面顯示如何運行hatch run lint:all
但這也適用於任何其他孵化命令:
# 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
# Run test in docker with specific Python version
./docker-hatch -v 3.9 test
安裝推薦的擴充功能。
配置孵化以在專案資料夾中建立虛擬環境。
hatch config set dirs.env.virtual .env
之後,透過執行hatch -e all run tests
來建立所需的所有 python 環境。
最後,設定 vscode 使用選定的環境之一: cmd + shift + p
-> python: Select Interpreter
-> 選擇./.env
中的資料夾之一
kagglehub 庫已配置自動日誌記錄,該日誌記錄儲存在日誌資料夾中。日誌目標透過 os.path.expanduser 解析
下表包含可能的位置:
作業系統 | 日誌路徑 |
---|---|
作業系統 | /user/$USERNAME/.kaggle/logs/kagglehub.log |
作業系統 | 〜/.kaggle/logs/kagglehub.log |
視窗 | C:Users%USERNAME%.kagglelogskagglehub.log |
請包含日誌以協助解決問題。