Google Colab - Not able to import API key file - google-colaboratory

I have a code in Google Colab which uses a Python package called Atlite, which in turn retrieves data from the Climate Data Store (CDS) through the use of an API key.
When running this code in Python I just need to have the file containing the key saved in a specific folder and then the code runs perfectly fine.
When I try to run the code in Google Colab the following error arises:
**Exception: Missing/incomplete configuration file: /root/.cdsapirc
**
I have the file ".cdsapirc" in my computer but when I try to import it to the "/root" folder in Google Colab it just does not get imported. I can import a .py file, but when I try to import the ".cdsapirc" file (which is basically a txt file) it does not work.
Could someone please help me to solve this issue?
Thank you!
Regards,
Sebastian

If uploading the .cdsapirc file doesn't work, you could try creating it inside Google Colab using a simple Python script:
uid = "<your uid>"
apikey = "<your api-key"
with open("/root/.cdsapirc", "w") as f:
print("url: https://cds.climate.copernicus.eu/api/v2", file=f)
print(f"key: {uid}:{apikey}", file=f)
You can get the uid and apikey either from CDS after logging in or you open you local .cdsapirc file and look them up there, see here for more information.
There might be a nicer solution by someone more familiar with Google Colab though.

Related

TypeError: 'module' object is not callable in Google Colab

I am getting the error when I run this code in google Colab
folders = glob('/content/drive/MyDrive/Data/train/*')
[![enter image description here]
You tried to use glob method directly, take the following code sample which was tested in Google colab environment. It should help to fix your issue.
from google.colab import drive
drive.mount('/gdrive')
import glob
path = glob.glob("/gdrive/MyDrive/*")
for file_or_folder in path:
print(file_or_folder)
For this issue, the path should be defined differently.
For that, initially import glob and drive module as mentioned below
from google.colab import drive
drive.mount('/gdrive')
import glob
And then using this, you can easily access the file as follows
folders = glob.glob('/gdrive/MyDrive/Datasets/Tomato/train/*')
Here if you see the difference in the copied path and the defined path is
'/content/drive/MyDrive/Datasets/Tomato/test' is what coppid form the drive
'/gdrive/MyDrive/Datasets/Tomato/train/*' is how you need to define the path

ImportError: cannot import name 'IEX_CLOUD_API_TOKEN' from 'secrets'

I'm facing the above error when I try to execute the following code:
from secrets import IEX_CLOUD_API_TOKEN
I tried using '.secrets' instead of 'secrets' but that gives the ModuleNotFoundError.
It's not a problem with the secrets library or anything of such kind.
It's just that after uploading secrets.py to the working directory, restart the kernel in your jupyter notebook.
Code shall run !!

bug (resolved) with "Mount drive" web-button in colab. Accessing "shared with me" files from google colab (y2020, previous solutions seem to fail)

[ Newer Edit]: colab team reported that they corrected the issue on May 27 2020.
I have checked - it works Okay for me now.
Link to issue: https://github.com/googlecolab/colabtools/issues/1205
==================================================================
[New Edit:] It became clear that problem below arises ONLY if mount the google drive to colab by via web interface button "Mount Drive"
and does NOT appear if mount by command line way.
So seems web way is bugged. See details in my own answer below.
It is checked for "Chrome" browser.
==================================================================
[Original question:]
How to access "shared with me" from google colab ? (Interface seems changed now (2020) and previously described solutions does not seem to work).
More details:
The question has been asked several times, and
the solutions described e.g. here : https://stackoverflow.com/a/53887376/625396
The problem that I do not see "Add to My Drive" , but see "Add shortcut to Drive".
After doing it, we can see that via web-interface for google drive, that shortcut indeed appears.
BUT that shortcut canNOT be seen via colab utilities, like
os.listdir() !
So shortcut seems to be invisible for colab, and not clear how to access it.
Below are the screenshot, showing that colab does not see the shortcut to "shared with me"-"cytotrace_datasets", but web-gui of google drive can see.
Here is screenshot what I see by colab (shortcut canNOT be seen):
Here is screenshot what I see by web-gui of google drive (shortcut can be seen):
Brief: do NOT mount google drive by web-interface button "Mount drive" (it is bugged), but do it in the "old" command line way, and you will not have problems.
Details:
After getting excellent answer above and playing with it,
it seems I found some strange thing which results in simpler solution and probably indicates that there is currently a bug with mounting the google drive by the web interface button "Mount drive".
I mean do NOT mount the drive by interface:
But do it in the old way:
and that is all - you will get the access to files which added before with the help of
"Add shortcut to Drive":
Suppose you want to read a shared csv file from drive. You have done "Add shortcut to Drive".
1) At Colab Notebook Connect to your drive.
# Import PyDrive and associated libraries.
# This only needs to be done once per notebook.
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
# Authenticate and create the PyDrive client.
# This only needs to be done once per notebook.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
2) Get the id of shared file you want to access.
Open file
-> go to linksharing [https://drive.google.com/open?id=1JKECh3GNry6xbAK6aBSzQtSntD4GTEl ] -> copy the the string after 'id='
3) back to colab
# A file ID looks like: laggVyWshwcyP6kEI-y_W3P8D26sz
file_id = '1JKECh3GNry6xbAK6aBSzQtSntMD4GTEl'
downloaded = drive.CreateFile({'id': file_id}) #important
print(downloaded['title']) # it should print the title of desired file
downloaded.GetContentFile('file.csv')
#Finally, you can read the file as pandas dataframe.
import pandas as pd
df= pd.read_csv('file.csv')
Note : This is my first ever answer to a stack overflow question

Can't import PDF with mpdf

everyone.
I started using mpdf on my website recently. Creating new pdfs is working fine, but i can't import existing ones. I get this error whenever i try to execute the import:
mPDF error: Cannot open ../folder1/folder2/folder3/folder4/folder5/thisisthepdf.pdf !
(that is not the real path)
I included the mpdf in the php. The folder and the files are on chmod 777 and the pdfs are all version 1.4
This is the way i am trying to import.
$mpdf=new mPDF();
$mpdf->SetImportUse();
$pagecount = $mpdf->SetSourceFile('../folder1/folder2/folder3/folder4/folder5/thisisthepdf.pdf');
$tplId = $mpdf->ImportPage($pagecount);
$mpdf->UseTemplate($tplId);
$mpdf->WriteHTML('Hallo World');
$mpdf->Output();
I tried various ways to import i found on stackoverflow and other sites, but nothing worked. Not even the code from the official mpdf manual i am using (the one above) is working.
Trying to solve this issue for quite a while now, but i am out of ideas. I Hope someone can help me. Thanks in advance!
This error message is raised because of a failing call of a simple fopen(). Which means that the PHP script simply cannot access the file.
So ensure that the path is valid by e.g. passing it to realpath(), as it seems to be a relative path. If this evaluates to false the path is simply wrong. Otherwise it is a permission issue.

Can download but file will not unzip as expected

I'm attempting to access the Geometadb database which first involves download of the SQL library. I did that and then I got the Geometadb library.
library(GEOmetadb)
Next I need the Geometadb file which is where things start to go wrong. I issue this command as seen exactly in the tutorial: https://bioconductor.riken.jp/packages/3.0/bioc/vignettes/GEOmetadb/inst/doc/GEOmetadb.html
if(!file.exists('GEOmetadb.sqlite')) getSQLiteFile()
It should proceed to not only download a .gz zip file but also unzip the file. It downloads it but never unzips it. Instead I get the following error.
trying URL 'http://dl.dropbox.com/u/51653511/GEOmetadb.sqlite.gz'
Error in download.file(url_geo, destfile = localfile, mode = "wb") :
cannot open URL 'http://dl.dropbox.com/u/51653511/GEOmetadb.sqlite.gz'
In addition: Warning message:
In url(url_geo_2, open = "rb") :
cannot open: HTTP status was '403 Forbidden'
Just not sure what's going on here. Considering these are just the early tutorial steps I'm probably missing something really obvious but I'm hoping someone can help me out. Thanks!