【问题标题】:Using Flask-RESTful in PyCharm在 PyCharm 中使用 Flask-RESTful
【发布时间】:2017-04-10 16:13:59
【问题描述】:

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

问题是,PyCharm 告诉我 ImportError:

没有名为 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 rest google-app-engine flask flask-restful


    【解决方案1】:

    你需要安装那个python包,你可以这样做:

    使用 pip 安装 Flask-RESTful

    pip install flask-restful

    开发版可从其 GitHub 页面下载。

    git 克隆https://github.com/flask-restful/flask-restful.git cd烧瓶-宁静 python 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 @987654322@ (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 "@987654323@" --2016-11-27 12:22:50-- @987654324@ 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 安装了模块并重新启动了一切,但不幸的是没有成功..
    • 由于virtualenv的隔离,你不应该使用“sudo”,这会将模块安装在你无法正常访问的全局环境中。如果你使用 Pycharm 最好是创建一个“requirement.txt”并在里面添加你的依赖项,然后 Pycharm 会自动安装它们。
    • 再次感谢!您的回答给出了深入了解库的 env 和目录的想法。事实证明,我的库都安装到 /Library/Python/2.7/site-packages/ (对不起,我是 Python 新手),但需要将我的项目文件夹复制到库中目录。现在它工作正常,但你知道我应该改变什么,所以库被安装到我的自定义项目文件夹中吗?
    • 您正在使用默认 python (2.7 macOS) 安装库。一般来说,您应该首先激活虚拟环境,然后使用 pip 将库安装到 venv。此外,您可以为 venv 设置项目解释器,并使用 PyCharm 对话框中的“+”来添加库。
    【解决方案2】:

    只是根据我自己的经验做一个简短的回答。你必须先安装这个模块

    sudo pip install flask-restful
    

    【讨论】:

      猜你喜欢
      • 2014-09-09
      • 1970-01-01
      • 2015-09-01
      • 2015-03-17
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      • 2018-02-22
      • 2015-05-14
      相关资源
      最近更新 更多