Search code examples
pycharmartificial-intelligencekaggle

How to import kaggle datasets to PyCharm IDE


I'm able to download kaggle using PIP command. Able to place the kaggle.json file into the appropriate folder. Able to see the competitions present in it. But when I'm trying to download the data files then an error getting displayed.

CODE:

import kaggle
from kaggle.api.kaggle_api_extended import KaggleApi
api = KaggleApi()
api.authenticate()
lis1 = api.competitions_list(search='LANL-Earthquake-Prediction')
api.competition_download_files('LANL-Earthquake-Prediction')

Error Message: Getting below error message.

Traceback (most recent call last):
  File "C:\Users\rpremala003\PycharmProjects\MachineLearning\Importing datasets\Importing Kaggle through API.py", line 11, in <module>
    api.competition_download_files('LANL-Earthquake-Prediction')
  File "C:\Users\rpremala003\PycharmProjects\MachineLearning\venv\lib\site-packages\kaggle\api\kaggle_api_extended.py", line 718, in competition_download_files
    self.competitions_data_download_files_with_http_info(
  File "C:\Users\rpremala003\PycharmProjects\MachineLearning\venv\lib\site-packages\kaggle\api\kaggle_api.py", line 400, in competitions_data_download_files_with_http_info
    return self.api_client.call_api(
  File "C:\Users\rpremala003\PycharmProjects\MachineLearning\venv\lib\site-packages\kaggle\api_client.py", line 329, in call_api
    return self.__call_api(resource_path, method,
  File "C:\Users\rpremala003\PycharmProjects\MachineLearning\venv\lib\site-packages\kaggle\api_client.py", line 161, in __call_api
    response_data = self.request(
  File "C:\Users\rpremala003\PycharmProjects\MachineLearning\venv\lib\site-packages\kaggle\api_client.py", line 351, in request
    return self.rest_client.GET(url,
  File "C:\Users\rpremala003\PycharmProjects\MachineLearning\venv\lib\site-packages\kaggle\rest.py", line 247, in GET
    return self.request("GET", url,
  File "C:\Users\rpremala003\PycharmProjects\MachineLearning\venv\lib\site-packages\kaggle\rest.py", line 241, in request
    raise ApiException(http_resp=r)
kaggle.rest.ApiException: (403)
Reason: Forbidden
HTTP response headers: HTTPHeaderDict({'Date': 'Sun, 04 Jul 2021 19:34:22 GMT', 'Content-Type': 'application/json', 'Transfer-Encoding': 'chunked', 'Set-Cookie': 'ka_sessionid=232614eb67b588938ac922774220f567; max-age=2626560; path=/, GCLB=CPDc0e-esJK-ZQ; path=/; HttpOnly', 'Vary': 'Accept-Encoding', 'Access-Control-Allow-Credentials': 'true', 'Turbolinks-Location': 'https://www.kaggle.com/api/v1/competitions/data/download-all/LANL-Earthquake-Prediction', 'X-Kaggle-MillisecondsElapsed': '91', 'X-Kaggle-RequestId': 'd485fdb0e4674834b5c1b444fd9885de', 'X-Kaggle-ApiVersion': '1.5.12', 'X-Frame-Options': 'SAMEORIGIN', 'Strict-Transport-Security': 'max-age=63072000; includeSubDomains; preload', 'Content-Security-Policy': "object-src 'none'; script-src 'nonce-q8ly/A24jHgL5gZmidMI6A==' 'report-sample' 'unsafe-inline' 'unsafe-eval' 'strict-dynamic' https: http:; frame-src 'self' https://www.kaggleusercontent.com https://www.youtube.com/embed/ https://polygraph-cool.github.io https://www.google.com/recaptcha/ https://form.jotform.com https://submit.jotform.us https://submit.jotformpro.com https://submit.jotform.com https://www.docdroid.com https://www.docdroid.net https://kaggle-static.storage.googleapis.com https://kaggle-static-staging.storage.googleapis.com https://kkb-dev.jupyter-proxy.kaggle.net https://kkb-staging.jupyter-proxy.kaggle.net https://kkb-production.jupyter-proxy.kaggle.net https://kkb-production.firebaseapp.com https://apis.google.com https://content-sheets.googleapis.com/ https://accounts.google.com/ https://storage.googleapis.com https://docs.google.com; base-uri 'none'; report-uri https://csp.withgoogle.com/csp/kaggle/20201130;", 'X-Content-Type-Options': 'nosniff', 'Referrer-Policy': 'strict-origin-when-cross-origin', 'Via': '1.1 google', 'Alt-Svc': 'clear'})
HTTP response body: b'{"code":403,"message":"You must accept this competition\\u0027s rules before you\\u0027ll be able to download files."}'

Solution

  • The error returned describes the root of the issue:

    HTTP response body: b'{"code":403,"message":"You must accept this competition\\u0027s rules before you\\u0027ll be able to download files."}'
    

    From the documentation of the Kaggle API:

    Just like participating in a Competition normally through the user interface, you must read and accept the rules in order to download data or make submissions. You cannot accept Competition rules via the API. You must do this by visiting the Kaggle website and accepting the rules there.

    If you have accepted the competition, perhaps you are not authenticating with a token corresponding to that user.