1

I'm currently learning Python (Flask) and would like to setup a tiny REST API for an HelloWorld. I choose flask_restful to implement the API and followed the tutorial on their website.

The problem is, that PyCharm is telling me the ImportError:

No module named flask_restful

although I implemented the library via the project interpreter in my 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?

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 -
Community
  • 1
  • 1
user3191334
  • 1,148
  • 3
  • 15
  • 33

2 Answers2

3

you need to install that python package, you can do it by:

Install Flask-RESTful with pip

pip install flask-restful

The development version can be downloaded from its page at GitHub.

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.

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" }

alainivars
  • 216
  • 2
  • 6
  • Thanks for your response! I installed the module using sudo pip install flask-restful and restarted everything, but unfortunately without success.. – user3191334 Nov 26 '16 at 14:55
  • Due to the isolation of virtualenv, you shouldn't use "sudo", that will install the module in global env where you don't have access normally. The better if you use Pycharm is to create a "requirement.txt" and add your dependencies inside, Pycharm will then install them automatically. – alainivars Nov 27 '16 at 11:57
  • Thanks again! Your answer gave the idea to take a deeper look at the env and directories of the libraries. It turns out, that my libraries were all installed to */Library/Python/2.7/site-packages/* (sorry I'm new in Python) but needed to be copied to my project folder into the library directory. Now it's working fine but do you know what I should change, so the libraries are installed to my custom project folder to? – user3191334 Nov 27 '16 at 12:21
  • You were installing libraries with default python (2.7 macOS). In general you should first activate virtual environment and then use pip to install libs to venv. Besides, you can setup project interpreter to the venv, and use "+" from that PyCharm dialog to add libraries. – emirc Nov 27 '16 at 12:50
0

Just a short answer from my own experience. You have to install this module first

sudo pip install flask-restful
Hafiz Muhammad Shafiq
  • 8,168
  • 12
  • 63
  • 121