2016-11-26 277 views
0

我目前正在学习Python(Flask),并且想为HelloWorld设置一个小型的REST API。我选择flask_restful来实施API,并在其网站上关注tutorial在PyCharm中使用Flask-RESTful

的问题是,PyCharm告诉我的导入错误:

No module named flask_restful

虽然我通过我的虚拟环境的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 - 

回答

2

您需要安装Python包,你可以做到这一点:

安装瓶的RESTful与PIP

PIP安装烧瓶宁静

开发版本可以从GitHub的页面下载。

混帐克隆https://github.com/flask-restful/flask-restful.git CD烧瓶宁静 蟒蛇的setup.py开发

一个好办法是也使用的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" }

+0

感谢您的回应!我安装了模块,使用sudo pip install flask-restful并重启所有内容,但不幸的是没有成功。 – user3191334

+0

由于virtualenv的隔离,你不应该使用“sudo”,它会在全局env中安装模块,通常无法访问。如果您使用Pycharm更好地创建一个“requirements.txt”并在其中添加您的依赖项,Pycharm将自动安装它们。 – alainivars

+0

再次感谢!你的回答给了我们更深入的了解图书馆的环境和目录的想法。事实证明,我的库全部安装到*/Library/Python/2.7/site-packages/*(对不起,我是Python新手),但需要将其复制到我的项目文件夹到库目录中。现在它工作正常,但你知道我应该改变什么,所以库被安装到我的自定义项目文件夹? – user3191334

0

只是从我自己的经验,一个简短的回答。您必须先安装此模块

sudo pip install flask-restful