【问题标题】:How to fix a TypeError saying 'module' object is not callable如何修复 TypeError 说“模块”对象不可调用
【发布时间】:2020-01-20 05:41:17
【问题描述】:

我的 wave.py 文件:

import User_Account

username = input('Username: ')
password = input('Password: ')

Session = User_Account(username, password)

我的 User_Account.py 文件:

import tidalapi

class User_Account:

    def __init__(self, username_, password_):
        self.username = username_
        self.password = password_

    def login(self):
        session = tidalapi.Session()
        return session.login(self.username, self.password)

当我在 PyCharm 中运行上述代码时,出现以下错误。

TypeError: 'module' 对象不可调用

我正在阅读 Python 中的 OOP 示例 - such as this - 即使我运行他们的代码,我也会遇到同样的错误。我在这里和谷歌上搜索过,但解决方案似乎无法解决问题。

关于我做错了什么有什么建议吗?

感谢您抽出宝贵时间,如果有什么我可以提供来改善我的问题,请不要犹豫。

编辑:完整追溯

Traceback (most recent call last):
  File "/home/doug/PycharmProjects/Wave/wave.py", line 6, in <module>
    Session = User_Account(username, password)
TypeError: 'module' object is not callable

【问题讨论】:

  • 这里有些问题,请提供完整的回溯。 import User_Account 不应该工作,因为模块名称都是小写的
  • 要从模块user_account导入类User_Account,你应该这样做from user_account import User_Account
  • 嗯,直到导入时区分大小写为platform-dependent
  • @roganjosh 为什么该图表显示“brrrrrrrrrr”?这是否意味着组合是不可能的?
  • @wjandrea 我不太确定。因为我才刚刚发现,所以有时间我需要玩一会儿,但我现在在玩手机

标签: python


【解决方案1】:

我认为问题在于您引入了模块,但没有从该模块中指定您想要的类。

我认为对 wave.py 进行以下更改可以解决此问题...

Session = User_Account.User_Account(username, password)

比导入 User_Account 更好,你可能想说...

from User_Account import User_Account

如果您这样做,您的“Session =”调用将按照您当前的方式工作。

【讨论】:

  • @DMellon:FWIW,PEP 8 coding guidelines 中的 naming conventions 用于包和模块表明“模块应该有简短的全小写名称”,这意味着你的 User_Account.py 脚本真的应该是重命名user_account.py
【解决方案2】:

就像错误所说的那样,模块不可调用。看起来您打算运行 from user_account import User_Account 而不是 import User_Account

请注意,import User_Account 在 Linux 等区分大小写的平台上会失败。如果你确实想导入user_account.py,你会写import user_account

【讨论】:

    猜你喜欢
    • 2015-02-15
    • 2017-02-07
    • 2017-06-17
    • 2014-09-21
    • 2011-05-30
    • 2021-11-22
    相关资源
    最近更新 更多