这有效:
from twilio import twiml
r = twiml.Response()
但
import twilio
r = twilio.twiml.Response()
失败了
AttributeError: 'module' object has no attribute 'twiml'
为什么?以及如何避免使用“从 twilio 导入东西”?
您可能还需要导入子模块:
import twilio.twiml
from twilio import twiml只有twiml在twilio模块的命名空间中才有效。如果它只是twiml.py在twilio目录中,它在twilio package中,但它不会在twilio 模块中,除非twilio模块将它导入到它的__init__.py.
有了所有这些背景信息,我认为您正在寻找的单线是这样的:
import twilio.twiml as twiml
这将twiml在twilio包中查找,然后将其作为twiml.