执行以下行时,

!pip install kaggle
!kaggle competitions download -c dogs-vs-cats -p /content/

我收到以下错误消息,
Traceback (most recent call last):
File "/usr/local/bin/kaggle", line 7, in <module>
from kaggle.cli import main
File "/usr/local/lib/python3.6/dist-packages/kaggle/__init__.py", line 23, in <module>
api.authenticate()
File "/usr/local/lib/python3.6/dist-packages/kaggle/api/kaggle_api_extended.py", line 109, in authenticate
self._load_config(config_data)
File "/usr/local/lib/python3.6/dist-packages/kaggle/api/kaggle_api_extended.py", line 151, in _load_config
raise ValueError('Error: Missing %s in configuration.' % item)
ValueError: Error: Missing username in configuration.

我不知道刚才发生了什么...以前相同的代码行得通。这是我第一次发现这个问题。

最佳答案

它突然也停止在这里工作。显然,kaggle api没有在正确的位置搜索kaggle.json文件。
由于我在colab笔记本中使用kaggle api,因此我要像这样导入kaggle.json:

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)

它工作得很好。但是现在,kaggle api在以下位置搜索kaggle.json:
~/.kaggle/kaggle.json

因此,我只需要将下载的文件移动/复制到正确的位置:
!mkdir ~/.kaggle
!cp /content/.kaggle/kaggle.json ~/.kaggle/kaggle.json

然后它又开始工作了。

关于python - 在Colab上导入Kaggle数据集时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51958553/

10-12 02:02