在 IBM WATSON STUDIO 中将脚本导入笔记本

Importing scripts into a notebook in IBM WATSON STUDIO

我在 IBM WATSON Studio 免费版的 CIFAR 10 图像上进行 PCA,所以我上传了 python 文件,用于在 studio 上下载 CIFAR10

下图。

但是当我尝试导入 cache 时,出现以下错误。 下图-

在 google 上花了一些时间后,我找到了解决方案,但我无法理解。 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>

这行我看不懂

` 并写入运行时会话附带的文件系统中的文件。``

在哪里可以找到运行时会话附带的文件?文件系统位于何处?

任何人都可以帮助我了解在哪里可以找到该文件的详细信息

您遇到导入错误,因为您尝试导入的脚本在 Python 运行时的本地文件系统中不可用。您上传的文件(cache.pycifar10.py 等)将上传到与 Watson Studio 项目关联的对象存储桶。要使用这些文件,您需要使它们对 Python 运行时可用,例如通过将脚本下载到运行时本地文件系统。

更新:同时有一个选项可以直接插入 StreamingBody 对象。这也将包含所有必需的凭据。如果您使用 insert StreamingBody object 选项,您可以跳到此答案的 writing it to a file in the local runtime filesystem 部分。

或者,

您可以使用下面的代码片段来读取 StreamingBody 对象中的脚本:

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 ) 

然后将其写入本地运行时文件系统中的文件。

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

这将打开一个具有写入权限的文件并调用 write 方法写入该文件。然后您应该能够简单地导入脚本。

import cifar

注意:您可以通过单击文件下拉菜单中的 Insert credentials 选项来获取文件的 IBM_API_KEY_ID 等凭据。

instructions that op found 遗漏了一行关键代码。我跟着他们,能够导入模块,但无法在这些模块中使用任何功能或 类。这是通过在写入后关闭文件来解决的。这部分在说明中:

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

应该改为(至少这对我有效):

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

希望这对某人有所帮助。