在IBM WATSON STUDIO中将脚本导入笔记本 [英] Importing scripts into a notebook in IBM WATSON STUDIO
问题描述
我正在IBM WATSON Studio Free版本的CIFAR 10映像上执行PCA,所以我上传了python文件,以便在Studio上下载CIFAR10
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
下面的图片.
但是当我尝试导入cache
时,显示以下错误.
图片如下
But when I trying to import cache
the following error is showing.
pic below-
在Google上花了一些时间后,我找到了解决方案,但我听不懂. 关联 https://dataplatform. cloud.ibm.com/docs/content/wsj/analyze-data/add-script-to-notebook.html
After spending some time on google I find a solution but I can't understand it. link https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/add-script-to-notebook.html
the solution is as follows:-
Click the Add Data icon (Shows the Add Data icon), and then browse the script file or drag it into your notebook sidebar.
Click in an empty code cell in your notebook and then click the Insert to code link below the file. Take the returned string, and write to a file in the file system that comes with the runtime session.
To import the classes to access the methods in a script in your notebook, use the following command:
For Python:
from <python file name> import <class name>
我听不懂这行
`并写入运行时会话随附的文件系统中的文件.``
` and write to a file in the file system that comes with the runtime session.``
在哪里可以找到运行时会话随附的文件?文件系统在哪里?
Where can I find the file that comes with runtime session? Where is the file system located?
请问有人可以在哪里找到该文件的详细信息吗?
Can anyone plz help me in this with the details where to find that file
推荐答案
您遇到导入错误,因为您要导入的脚本在Python运行时的本地文件系统中不可用.您上载的文件(cache.py
,cifar10.py
等)被上载到与Watson Studio项目关联的对象存储桶.要使用这些文件,您需要使它们可用于Python运行时,例如,将脚本下载到运行时本地文件系统.
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.
更新:同时,有一个选项可以直接插入StreamingBody对象.这还将包括所有必需的凭据.如果您使用的是insert StreamingBody object
选项,则可以跳至此答案的writing it to a file in the local runtime 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.
或者,
您可以使用下面的代码片段读取StreamingBody对象中的脚本:
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())
这将打开一个具有写访问权的文件,并调用write
方法来写入该文件.然后,您应该能够简单地导入脚本.
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
注意:通过单击文件下拉菜单上的Insert credentials
选项,可以获取文件的凭据,例如IBM_API_KEY_ID
.
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.
这篇关于在IBM WATSON STUDIO中将脚本导入笔记本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!