【问题标题】:Why I am getting this error: object has no attribute 'astype'为什么我收到此错误:对象没有属性“astype”
【发布时间】:2018-01-13 07:47:28
【问题描述】:

我在 python 文件myfile 中定义了一个函数myfunc。当我从 jupyter notebook 导入这个函数时,它显示以下错误:

import numpy as np
import os
from scipy.misc import imread
import ast
from myfile import myfunc
 .....
class_mask = np.equal(image, i)
class_mask = class_mask.astype(np.float32)
 .....
AttributeError: 'NotImplementedType' object has no attribute 'astype'

但是,每当我在单元格中拥有 myfile 内容并在 jupyter 中运行时,它是否可以正常工作?这个错误的原因是什么? 谢谢

【问题讨论】:

    标签: python python-2.7 jupyter-notebook


    【解决方案1】:

    我们只能从 python 模块/文件中导入函数。保存时的 Jupyter Notebook 扩展名为 .pynb,这意味着它实际上不是 python 模块。

    只需使用文本编辑器打开您的笔记本,您可能会看到类似 html 的内容。 python 模块不可能包含的内容。

    您宁愿将 Jupyter Notebook 作为 Python 模块导出到您尝试运行当前程序的同一目录中。

    文件 -> 导出为 Python 文件

    导出后,您可以将其用作任何其他 python 模块。希望对您有所帮助!

    【讨论】:

    • 感谢您的评论,我的代码位于 python 文件中,而不是 jupyter,python 代码和 jupyter 模块位于我尝试导入并从 jupyter 代码调用它的同一目录中。
    • 尝试从myfile import myfile.myfunc
    • 好的,谢谢穆库尔
    • 没有 Mukul,感谢您的帮助
    猜你喜欢
    • 2023-02-05
    • 2023-03-15
    • 2020-01-08
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    相关资源
    最近更新 更多