gpt4 book ai didi

python - falcon 中的对象没有属性 'API' 错误

转载 作者:行者123 更新时间:2023-12-02 04:01:29 25 4
gpt4 key购买 nike

我正在 ubuntu 14.04 上使用 python 2.7.6 和 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'

最佳答案

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

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


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

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.

关于python - falcon 中的对象没有属性 'API' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41678116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com