【问题标题】:Trying to use "import twilio" instead of "from twilio import twiml"尝试使用“import twilio”而不是“from twilio import twiml”
【发布时间】:2012-05-29 23:59:56
【问题描述】:

这行得通:

from twilio import twiml
r = twiml.Response()

但是

import twilio
r = twilio.twiml.Response()

失败

AttributeError: 'module' object has no attribute 'twiml'

为什么?以及如何避免使用“from twilio import stuff”?

【问题讨论】:

    标签: python twilio


    【解决方案1】:

    您可能还需要导入子模块:

    import twilio.twiml
    

    【讨论】:

    • 对。只有当顶级 twilio 模块导入 twiml 时,它才会以 OP 想要的方式可用。
    【解决方案2】:

    from twilio import twiml 仅在 twiml 位于 twilio 模块的命名空间中时才有效。如果它只是twilio 目录中的twiml.py,它在twilio 中,但它不会在twilio 模块 中,除非@ 987654328@ 模块将其导入其__init__.py

    有了所有这些背景信息,我认为您正在寻找的单线是这样的:

    import twilio.twiml as twiml
    

    这将在twilio 包中查找twiml,然后将其作为twiml 带入您的命名空间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      相关资源
      最近更新 更多