在 PyCharm 中使用 Flask-RESTful

Using Flask-RESTful in PyCharm

我目前正在学习 Python (Flask) 并想为 HelloWorld 设置一个小型 REST API。我选择 flask_restful 来实施 API 并在他们的网站上关注 tutorial

问题是,PyCharm 告诉我 ImportError:

No module named flask_restful

尽管我通过我的 VirtualEnvironment 中的 project interpreter 实现了库。

这是我的代码:

from flask import Flask
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

class HelloWorld(Resource):
    def get(self):
        return {'hello': 'world'}

api.add_resource(HelloWorld, '/')

if __name__ == '__main__':
    app.run(debug=True)

有人知道正确使用 flask_restful 的窍门吗?

INFO     2016-11-26 13:25:04,657 admin_server.py:116] Starting admin server at: http://localhost:8000
ERROR    2016-11-26 13:25:07,163 wsgi.py:263] 
Traceback (most recent call last):
  File "/Users/GamerXX/Documents/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/Users/GamerXX/Documents/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/Users/GamerXX/Documents/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "/Users/GamerXX/PycharmProjects/PartyMate/main.py", line 3, in <module>
    from flask_restful import Resource, Api
ImportError: No module named flask_restful
INFO     2016-11-26 13:25:07,169 module.py:788] default: "GET / HTTP/1.1" 500 -

您需要安装 python 软件包,您可以通过以下方式安装:

使用 pip 安装 Flask-RESTful

pip install flask-restful

开发版可以在GitHub页面下载。

git 克隆 https://github.com/flask-restful/flask-restful.git CD 烧瓶-restful python setup.py 发展

一个好方法是同时使用 virtualenv 将 python 包依赖项从一个项目分离到另一个项目。

我只是测试它,它可以在 Ubuntu 16.04: 上运行 <em>~/repositories$ virtualenv venv_flask_restful</em> venv_flask_restful/bin/python2.7 中的新 python 可执行文件 还在 venv_flask_restful/bin/python 中创建可执行文件 安装 setuptools、pip、wheel...完成。 <em>~/repositories$ 来源venv_flask_restful/bin/activate</em><br> <em>(flask_restful) ~/repositories$ pip install flask-restful</em><br> ... <em>~/repositories$ pip freeze</em> aniso8601==1.2.0 ... 烧瓶==0.11.1 Flask-RESTful==0.3.5 ... <em>~/repositories$pythontest_flask_restful.py</em> * 运行 on <a href="http://127.0.0.1:5000/" rel="nofollow noreferrer">http://127.0.0.1:5000/</a>(按 CTRL+C 退出) *重新启动统计 * 调试器已激活! *调试器密码:106-365-003 "The wget command on other term is launch at that time" 127.0.0.1 - - [27/Nov/2016 12:18:55] "GET / HTTP/1.1" 200 - <strong>在另一个终端上:</strong> <em>~$ wget -c --read-timeout=5 --tries=0 "<a href="http://127.0.0.1:5000/" rel="nofollow noreferrer">http://127.0.0.1:5000/</a>"</em> --2016-11-27 12:22:50-- <a href="http://127.0.0.1:5000/" rel="nofollow noreferrer">http://127.0.0.1:5000/</a> Connexion à 127.0.0.1:5000…连接。 请求 HTTP 传输,en attente de la reponse ... 200 OK <em>~$猫index.html</em> { "hello": "world" }

根据我自己的经验简单回答一下。您必须先安装此模块

sudo pip install flask-restful