2017-01-16 73 views
0

我在ubuntu 14.04上使用python 2.7.6與falcon web框架並嘗試運行簡單的Hello World程序。但是在運行這個例子時會出現以下錯誤。對此有何想法?對象在falcon中沒有屬性「API」錯誤

代碼:

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

你可能命名爲您的Python文件falcon.py吧? – iFlo

回答

2

你的Python文件falcon.py所以當你打電話falcon.API()你調用一個方法API()在你的文件,而不是從真正的獵鷹模塊。

只需重命名您的文件,它將工作。


更完整的解決方案,請參閱本:

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

You will want to read about Absolute and Relative Imports which addresses this very problem. Use:

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

need to use relative imports (from .email import ...) to access your own package.

相關問題