Kaggle API Issue "Could Not Find Kaggle.JSON. Make Sure It's Located In......"

Can't find kaggle.json file in google colab

As the error said, you need to put kaggle.json in the right place.

Try:

!mv .kaggle /root/

Then run your code again.

Kaggle API issue Could not find kaggle.json. Make sure it's located in......

I have solved this. For some reason the following command works and not the one posted above.

~/.local/bin/kaggle competitions download -c home-credit-default-risk

Download a Kaggle Dataset

Maybe this post helps: https://www.kaggle.com/general/51898
it links to this script:

# Info on how to get your api key (kaggle.json) here: https://github.com/Kaggle/kaggle-api#api-credentials
!pip install kaggle
api_token = {"username":"USERNAME","key":"API_KEY"}
import json
import zipfile
import os
with open('/content/.kaggle/kaggle.json', 'w') as file:
json.dump(api_token, file)
!chmod 600 /content/.kaggle/kaggle.json
!kaggle config path -p /content
!kaggle competitions download -c jigsaw-toxic-comment-classification-challenge
os.chdir('/content/competitions/jigsaw-toxic-comment-classification-challenge')
for file in os.listdir():
zip_ref = zipfile.ZipFile(file, 'r')
zip_ref.extractall()
zip_ref.close()

from: https://gist.github.com/jayspeidell/d10b84b8d3da52df723beacc5b15cb27

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.

cannot move Kaggle.json to /root/.kaggle in Colab

Problem solved. I found asking on StackOverflow is really a good way to find solutions!!! Yeah, time saving!!!

When I was typing the question, I found that since it says !mv ... not a directory and !ls no such file or directory, maybe there is no folder of .kaggle.

I tried to create the folder and just follow the instructions if any error happens

!pip install kaggle -q      # At first, I suspect the kaggle API lose effect so it doesn't have .kaggle folder. (not working)
!rm -rf /root/.kaggle. # when I created the folder, it says the file or dir already exits
!mkdir /root/.kaggle # successful
!mv kaggle.json /root/.kaggle/kaggle.json # not sure if I have to use full destination path, I previously only used /root/.kaggle and it failed. Don't have time to validate this thought.
!ls /root/.kaggle/kaggle.json
!kaggle datasets download muerbingsha/pandatiledsingle

Conclusion:

The Colab environment changed that .kaggle is lost for unknown reasons.



Related Topics



Leave a reply



Submit