对象在猎鹰中没有属性'API'错误

时间:2017-01-16 14:01:40

标签: python falconframework falcon

我在python 2.7.6上使用ubuntu 14.04 falcon web框架并尝试运行简单的hello world程序。但是在运行此示例时它会出现以下错误。对此有何想法?

代码:

import falcon

class ThingsResource(object):
    def on_get(self, req, resp):
        """Handles GET requests"""
        resp.status = falcon.HTTP_200
        resp.body = 'Hello world!'

# falcon.API instances are callable WSGI apps
wsgi_app = api = falcon.API()

# Resources are represented by long-lived class instances
things = ThingsResource()

# things will handle all requests to the '/things' URL path
api.add_route('/hello', things)

错误:

Traceback (most recent call last):
  File "falcon.py", line 1, in <module>
    import falcon
  File "/home/naresh/Desktop/PythonFramework/falcon.py", line 10, in <module>
    wsgi_app = api = falcon.API()
AttributeError: 'module' object has no attribute 'API'

1 个答案:

答案 0 :(得分:2)

你的python文件是falcon.py所以当你调用falcon.API()时,你在文件中调用方法API(),而不是真正的猎鹰模块。

只需重命名您的文件即可。

有关更完整的解决方案,请参阅:

Trying to import module with the same name as a built-in module causes an import error

  

您需要阅读有关绝对和相对进口的内容   解决了这个问题。使用:

from __future__ import absolute_import Using that, any unadorned package name will always refer to the top level package. You will then
     

需要使用相对导入(来自.email import ...)来访问你的   自己的包裹。