【问题标题】:AttributeError: module 'test' has no attribute 'myfunc'AttributeError:模块“测试”没有属性“myfunc”
【发布时间】:2020-12-04 15:23:32
【问题描述】:

我有一个类,它在他的一个函数中从我创建的另一个文件中调用另一个函数:

import test
class myclass:
    ...
    def mainfunc(self):
        test.myfunc()
    ...
start = myclass()
start.mainfunc()

但它给了我这个错误:

AttributeError: 模块 'test' 没有属性 'myfunc'

而且我不知道它为什么不调用它,即使是我正在使用它的编辑器也会自动为我完成该功能。

我该如何解决这个问题?

【问题讨论】:

  • 请提供一些关于test 的信息

标签: python python-3.x


【解决方案1】:

测试模块(https://docs.python.org/3/library/test.html#module-test)用于内部python回归测试,可能是您正在导入该模块,而不是您的同名类。

【讨论】:

    【解决方案2】:

    只需将导入语句更改为

    from test import myfunc
    

    并在mainfunc 内部使用

    myfunc()
    

    此解决方案假设test 内部有一个函数myfunc

    【讨论】:

    • 如果test 模块中没有函数myfunc(),那么from test import myfunc 将如何提供帮助?
    猜你喜欢
    • 1970-01-01
    • 2014-10-23
    • 2017-08-01
    • 2018-04-14
    • 2019-02-18
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 2019-07-20
    相关资源
    最近更新 更多