Error while importing Kaggle dataset on Colab
It suddenly stopped working here as well. Apparently, the kaggle api was not searching the kaggle.json file in the correct place. Since I was using the kaggle api inside a colab notebook, I was importing the kaggle.json like this:
from googleapiclient.discovery import build
import io, os
from googleapiclient.http import MediaIoBaseDownload
from google.colab import auth
auth.authenticate_user()
drive_service = build('drive', 'v3')
results = drive_service.files().list(
q="name = 'kaggle.json'", fields="files(id)").execute()
kaggle_api_key = results.get('files', [])
filename = "/content/.kaggle/kaggle.json"
os.makedirs(os.path.dirname(filename), exist_ok=True)
request = drive_service.files().get_media(fileId=kaggle_api_key[0]['id'])
fh = io.FileIO(filename, 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress() * 100))
os.chmod(filename, 600)
It worked just fine. But now, the kaggle api searches the kaggle.json in this location:
~/.kaggle/kaggle.json
So, I just had to move/copy the file I downloaded to the right place:
!mkdir ~/.kaggle
!cp /content/.kaggle/kaggle.json ~/.kaggle/kaggle.json
And it started working again.
This simple thing did it for me on Google Cola.
!echo '{"username":"USERNAME","key":"KEY"}' > ~/.kaggle/kaggle.json
!kaggle datasets download -d mmoreaux/environmental-sound-classification-50
--
edit, might have changed to:
!echo '{"username":"USERNAME","key":"KEY"}' > /root/.kaggle/kaggle.json
!kaggle datasets download -d mmoreaux/environmental-sound-classification-50
Initially had trouble copying the .json file into the colab VM. Eventually for me the following worked: working through google colaboratory, first you need to install the kaggle API with:
!pip install kaggle
Further information and instructions here https://github.com/Kaggle/kaggle-api. Next, the link instructs you to activate the API with a file you can download with your kaggle user on kaggle.com -> My account -> create new API token. this file is kaggle.json.
Next, in order to upload this kaggle.json file to the colab VM for activation, you can upload it first to your google drive (simply drag it to your drive). Next enter the following command in colab to import your drive:
from google.colab import drive
drive.mount('/content/gdrive')
after authorization is completed, you can copy the file from the drive to colab with:
!cp /content/gdrive/My\ Drive/kaggle.json ~/.kaggle/kaggle.json
And Finally, hopefully you will be able to run the command:
!kaggle competitions download -c <competition-name>
I hope this helps!