Skip to content Skip to sidebar Skip to footer

Importing Scripts Into A Notebook In IBM WATSON STUDIO

I am doing PCA on CIFAR 10 image on IBM WATSON Studio Free version so I uploaded the python file for downloading the CIFAR10 on the studio pic below. But when I trying to import

Solution 1:

You have the import error because the script that you are trying to import is not available in your Python runtime's local filesystem. The files (cache.py, cifar10.py, etc.) that you uploaded are uploaded to the object storage bucket associated with the Watson Studio project. To use those files you need to make them available to the Python runtime for example by downloading the script to the runtimes local filesystem.

UPDATE: In the meanwhile there is an option to directly insert the StreamingBody objects. This will also have all the required credentials included. You can skip to writing it to a file in the local runtime filesystem section of this answer if you are using insert StreamingBody object option.

enter image description here

Or,

You can use the code snippet below to read the script in a StreamingBody object:

import types
import pandas as pd
from botocore.client import Config
import ibm_boto3

def __iter__(self): return 0
os_client= ibm_boto3.client(service_name='s3',
ibm_api_key_id='<IBM_API_KEY_ID>',
ibm_auth_endpoint="<IBM_AUTH_ENDPOINT>",
config=Config(signature_version='oauth'),
endpoint_url='<ENDPOINT>')

# Your data file was loaded into a botocore.response.StreamingBody object.
# Please read the documentation of ibm_boto3 and pandas to learn more about the possibilities to load the data.
# ibm_boto3 documentation: https://ibm.github.io/ibm-cos-sdk-python/
# pandas documentation: http://pandas.pydata.org/
streaming_body_1 = os_client.get_object(Bucket='<BUCKET>', Key='cifar.py')['Body']
# add missing __iter__ method, so pandas accepts body as file-like object
if not hasattr(streaming_body_1, "__iter__"): streaming_body_1.__iter__ = types.MethodType( __iter__, streaming_body_1 ) 

And then write it to a file in the local runtime filesystem.

f = open('cifar.py', 'wb')
f.write(streaming_body_1.read())

This opens a file with write access and calls the write method to write to the file. You should then be able to simply import the script.

import cifar

Note: You can get the credentials like IBM_API_KEY_ID for the file by clicking on the Insert credentials option on the drop-down menu for your file.


Solution 2:

The instructions that op found miss one crucial line of code. I followed them and was able to import modules but wasn't able to use any functions or classes in those modules. This was fixed by closing the files after writing. This part in the instrucitons:

f = open('<myScript>.py', 'wb')
f.write(streaming_body_1.read())

should instead be (at least this works in my case):

f = open('<myScript>.py', 'wb')
f.write(streaming_body_1.read())
f.close()

Hopefully this helps someone.


Post a Comment for "Importing Scripts Into A Notebook In IBM WATSON STUDIO"