简体   繁体   English

在PyCharm中使用Flask-RESTful

[英]Using Flask-RESTful in PyCharm

I'm currently learning Python (Flask) and would like to setup a tiny REST API for an HelloWorld. 我目前正在学习Python(Flask),并希望为HelloWorld设置一个小的REST API。 I choose flask_restful to implement the API and followed the tutorial on their website. 我选择flask_restful来实现API并按照他们网站上的教程进行操作。

The problem is, that PyCharm is telling me the ImportError: 问题是,PyCharm告诉我ImportError:

No module named flask_restful 没有名为flask_restful的模块

although I implemented the library via the project interpreter in my VirtualEnvironment. 虽然我通过我的VirtualEnvironment中的项目解释器实现了库。

This is my code: 这是我的代码:

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)

Does anybody know the trick, to use flask_restful correctly? 有没有人知道这个技巧,正确使用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 -

you need to install that python package, you can do it by: 你需要安装那个python包,你可以通过以下方式完成:

Install Flask-RESTful with pip 使用pip安装Flask-RESTful

pip install flask-restful pip安装烧瓶 - 安静

The development version can be downloaded from its page at GitHub. 开发版本可以从GitHub的页面下载。

git clone https://github.com/flask-restful/flask-restful.git cd flask-restful python setup.py develop git clone https://github.com/flask-restful/flask-restful.git cd flask-restful python setup.py develop

A good way is to also use virtualenv to separate python package dependencies from one to an other project. 一个好方法是使用virtualenv将python包依赖项从一个项目分离到另一个项目。

I just test it and it work on Ubuntu 16.04: ~/repositories$ virtualenv venv_flask_restful New python executable in venv_flask_restful/bin/python2.7 Also creating executable in venv_flask_restful/bin/python Installing setuptools, pip, wheel...done. ~/repositories$ source venv_flask_restful/bin/activate
(flask_restful) ~/repositories$ pip install flask-restful
... ~/repositories$ pip freeze aniso8601==1.2.0 ... Flask==0.11.1 Flask-RESTful==0.3.5 ... ~/repositories$ python test_flask_restful.py * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger pin code: 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 - ON AN OTHER TERMINAL: ~$ wget -c --read-timeout=5 --tries=0 " http://127.0.0.1:5000/ " --2016-11-27 12:22:50-- http://127.0.0.1:5000/ Connexion à 127.0.0.1:5000… connecté. requête HTTP transmise, en attente de la réponse… 200 OK ~$ cat index.html { "hello": "world" }
我只测试它并且它可以在Ubuntu 16.04上运行: ~/repositories$ virtualenv venv_flask_restful New python executable in venv_flask_restful/bin/python2.7 Also creating executable in venv_flask_restful/bin/python Installing setuptools, pip, wheel...done. ~/repositories$ source venv_flask_restful/bin/activate
(flask_restful) ~/repositories$ pip install flask-restful
... ~/repositories$ pip freeze aniso8601==1.2.0 ... Flask==0.11.1 Flask-RESTful==0.3.5 ... ~/repositories$ python test_flask_restful.py * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger pin code: 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 - ON AN OTHER TERMINAL: ~$ wget -c --read-timeout=5 --tries=0 " http://127.0.0.1:5000/ " --2016-11-27 12:22:50-- http://127.0.0.1:5000/ Connexion à 127.0.0.1:5000… connecté. requête HTTP transmise, en attente de la réponse… 200 OK ~$ cat index.html { "hello": "world" }
~/repositories$ virtualenv venv_flask_restful New python executable in venv_flask_restful/bin/python2.7 Also creating executable in venv_flask_restful/bin/python Installing setuptools, pip, wheel...done. ~/repositories$ source venv_flask_restful/bin/activate
(flask_restful) ~/repositories$ pip install flask-restful
... ~/repositories$ pip freeze aniso8601==1.2.0 ... Flask==0.11.1 Flask-RESTful==0.3.5 ... ~/repositories$ python test_flask_restful.py * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger pin code: 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 - ON AN OTHER TERMINAL: ~$ wget -c --read-timeout=5 --tries=0 " http://127.0.0.1:5000/ " --2016-11-27 12:22:50-- http://127.0.0.1:5000/ Connexion à 127.0.0.1:5000… connecté. requête HTTP transmise, en attente de la réponse… 200 OK ~$ cat index.html { "hello": "world" }

Just a short answer from my own experience. 从我自己的经验中得到一个简短的答案。 You have to install this module first 您必须先安装此模块

sudo pip install flask-restful

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM