簡體   English   中英

在PyCharm中使用Flask-RESTful

[英]Using Flask-RESTful in PyCharm

我目前正在學習Python(Flask),並希望為HelloWorld設置一個小的REST API。 我選擇flask_restful來實現API並按照他們網站上的教程進行操作。

問題是,PyCharm告訴我ImportError:

沒有名為flask_restful的模塊

雖然我通過我的VirtualEnvironment中的項目解釋器實現了庫。

這是我的代碼:

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安裝燒瓶 - 安靜

開發版本可以從GitHub的頁面下載。

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

一個好方法是使用virtualenv將python包依賴項從一個項目分離到另一個項目。

我只測試它並且它可以在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" }

從我自己的經驗中得到一個簡短的答案。 您必須先安裝此模塊

sudo pip install flask-restful

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM