3

I am new to python and flask. I want to use flask-restplus. On using flask restplus, I am unable to start my app with "flask run" command. However, it does start when I run it as module "python3 -m flask run". My first question is there any difference starting it as a module vs the normal way? Secondly, what's the reason it doesn't start with "flask run" in this case. Am I missing something here?

Here is a simple sample app with flask restplus:

from flask import Flask
from flask_restplus import Resource, Api

app = Flask(__name__)                  #  Create a Flask WSGI application
api = Api(app)                         #  Create a Flask-RESTPlus API

@api.route('/hello')                   #  Create a URL route to this resource
class HelloWorld(Resource):            #  Create a RESTful resource
    def get(self):                     #  Create GET endpoint
        return {'hello': 'world'}

if __name__ == '__main__':
    app.run()

Here is the output of "flask run" command:

(venv) ~/C/e/s/testpython ❯❯❯ flask run
 * Serving Flask app "app.py"
 * Environment: local
 * Debug mode: off
Usage: flask run [OPTIONS]

Error: While importing "app", an ImportError was raised:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 235, in locate_app
    __import__(module_name)
  File "/Users/rajatarora/Code/eon/sales-plus/testpython/app.py", line 2, in <module>
    from flask_restplus import Resource, Api
ModuleNotFoundError: No module named 'flask_restplus'

and the output using "python3 -m flask run" command:

(venv) ~/C/e/s/testpython ❯❯❯ python3 -m flask run                                  ✘ 2
 * Serving Flask app "app.py"
 * Environment: local
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

flask resplus is ofcourse installed. Here is output of pip freeze

(venv) ~/C/e/s/testpython ❯❯❯ pip freeze
aniso8601==8.0.0
attrs==19.3.0
Click==7.0
Flask==1.1.1
flask-restplus==0.13.0
importlib-metadata==0.23
itsdangerous==1.1.0
Jinja2==2.10.3
jsonschema==3.2.0
MarkupSafe==1.1.1
more-itertools==7.2.0
pyrsistent==0.15.5
pytz==2019.3
six==1.13.0
Werkzeug==0.16.0
zipp==0.6.0
Rajat Arora
  • 582
  • 1
  • 6
  • 18

2 Answers2

3

The flask-restplus module is not installed. You can install it from a virtualenv environment or can be local (not recommended, "but is your choose").

pip install flask-restplus

# To see if module is installed
pip freeze

The link with infos about intall flask-restplus is

[https://flask-restplus.readthedocs.io/en/stable/installation.html][1]
willteam
  • 117
  • 7
  • it's installed. thats what confused me. I updated the question with pip freeze output. You can try the same on your machine. – Rajat Arora Nov 19 '19 at 12:29
  • Is possible that you need run the `pip3` to install your libs, instead of `pip`. Because how to I can see do you needs run flask with python3, your default version of python is python2, with this, the same will happens with pip...pip will run a version compatible with python2 <--the default python version in your envirioment...so, do you need install by pip3... Execute a pip --version to confirm this information. The result will by like that: `pip 19.3.1 from /opt/myenv/local/lib/python3.5/site-packages/pip (python 3.5)`, and after pip3 --version – willteam Nov 19 '19 at 12:44
  • thank you again. I am installing the dependencies in virtualenv with python3. pip --version and pip3 --version return the same for me. pip 19.3.1 from /Users/*/Code/testpython/venv/lib/python3.7/site-packages/pip (python 3.7) However, The problem is solved for me if i do deactivate the python virtualenv once and activate it again. Afterward, everything works fine. Not sure why though :/ – Rajat Arora Nov 19 '19 at 14:49
0

on command line: try if windows: set FLASK_APP=app.py and then you run the flask run command.