Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google colab file download failed to fetch error

I used google colab to make a dictionary, dump it into a json file and download the file into my laptop by this code:

from google.colab import files
import json

dict = {'apple': 'fruit', 'mango': 'fruit', 'carrot': 'vegetable', 'brocoli': 'vegetable', 'cat': 'animal'}

with open('sampleDictionary.json', 'w') as f:
  json.dump(dict, f)

files.download('sampleDictionary.json')
f.close()

When I try to run this code, it gives this error:

MessageError                              Traceback (most recent call last)
<ipython-input-29-1251d71a0a36> in <module>()
      7   json.dump(dict, f)
      8 
----> 9 files.download('sampleDictionary.json')
     10 f.close()

/usr/local/lib/python3.6/dist-packages/google/colab/files.py in download(filename)
    176       'port': port,
    177       'path': _os.path.abspath(filename),
--> 178       'name': _os.path.basename(filename),
    179   })

/usr/local/lib/python3.6/dist-packages/google/colab/output/_js.py in eval_js(script, ignore_result)
     37   if ignore_result:
     38     return
---> 39   return _message.read_reply_from_input(request_id)
     40 
     41 

/usr/local/lib/python3.6/dist-packages/google/colab/_message.py in read_reply_from_input(message_id, timeout_sec)
    104         reply.get('colab_msg_id') == message_id):
    105       if 'error' in reply:
--> 106         raise MessageError(reply['error'])
    107       return reply.get('data', None)
    108 

MessageError: TypeError: Failed to fetch

Click here to see the screenshot of my code and the error

Please help me out

like image 948
Suraj S Jain Avatar asked Dec 02 '18 14:12

Suraj S Jain


People also ask

How can I download data directly to Colab?

You can use the upload option at the top of the Files explorer to upload any file(s) from your local machine to Google Colab. Step 2: Click the upload icon and select the file(s) you wish to upload from the “File Upload” dialog window.

How do I download a Google collab file?

How to Download Folders from Colab. The command is !zip followed by r which means “recursive”, then we write the file path of the zipped file (i.e. /content/sample_data. zip ) and finally, we write the folder that we want to zip (i.e. /content/sample_data ) and voila, the zip file is generated :-).


4 Answers

you need to enable third-party cookies but for now it only works for Chrome browser, open

chrome://settings/content/cookies

make sure the option for Block third-party cookies is disabled and click add button in Allow section then add

colab.research.google.com
like image 66
ewwink Avatar answered Oct 12 '22 08:10

ewwink


I encountered the same problem (MessageError: TypeError: Failed to fetch) while using colab.

then, I split file operations into different code units in a colab notebook; I put file open, write, close in one code unit, and use files.download() in the subsequent code unit.

the problem is gone!

like image 27
ShuehCheng Hu Avatar answered Oct 12 '22 08:10

ShuehCheng Hu


The problem is that the file is not finished being written by the time google attempts to "fetch" the file.
Simple solution:

with open('sampleDictionary.json', 'w') as f:
  json.dump(dict, f)
time.sleep(10)
files.download('sampleDictionary.json')

More complicated solution could be put a for loop with a try catch statement for files.download, and then put a sleep in the catch. Keep a max loop time in case the file is never completed.

like image 44
blackdrumb Avatar answered Oct 12 '22 10:10

blackdrumb


After trying chrome cookies, tried except loop, nothing worked for me, so I changed the way that I get my files. I used google drive file stream, it was very easy and efficient:

from google.colab import drive
drive.mount('/content/drive')

it will ask you for authorization code, you can find it after visiting the corresponding url.

with open('/content/drive/My Drive/foo.txt', 'w') as f:
  f.write('Hello Google Drive!')
  #other instructions

drive.flush_and_unmount()
like image 42
medchik Avatar answered Oct 12 '22 10:10

medchik