No module named 'flask_httpauth' but Requirement already satisfied

1,827 views
Skip to first unread message

Marchello Lippi

unread,
Feb 1, 2021, 3:50:24 AM2/1/21
to Google App Engine
Hi all, 

My GAE flask web app works fine without any auth, but when I try to add basic authentication, it starts to produce 502 Bad Gateway error. Looked into GAE logs, found error: 

> from flask_httpauth import HTTPBasicAuth
ModuleNotFoundError: No module named 'flask_httpauth'

Ok, the module might be not installed yet, so: 

> $ pip3 install flask_httpauth
Requirement already satisfied: flask_httpauth in ./.local/lib/python3.7/site-packages (4.2.0)
Requirement already satisfied: Flask in /usr/local/lib/python3.7/dist-packages (from flask_httpauth) (1.1.2)
Requirement already satisfied: click>=5.1 in /usr/local/lib/python3.7/dist-packages (from Flask->flask_httpauth) (7.0)
Requirement already satisfied: Werkzeug>=0.15 in /usr/local/lib/python3.7/dist-packages (from Flask->flask_httpauth) (1.0.1)
Requirement already satisfied: itsdangerous>=0.24 in /usr/local/lib/python3.7/dist-packages (from Flask->flask_httpauth) (1.1.0)
Requirement already satisfied: Jinja2>=2.10.1 in /usr/local/lib/python3.7/dist-packages (from Flask->flask_httpauth) (2.11.2)
Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python3.7/dist-packages (from Jinja2>=2.10.1->Flask->flask_httpauth) (1.1.1)

As you can see, it was already installed before. 

Just in case, installed it for pip (not only for pip3)

> $ pip install flask_httpauth

After that I tried to call my web app again 


but it produces the same error as above. 

(also its code is below for your convenience)

Please advise. 


import flask
from flask import Flask
from flask import jsonify
from Flask_HTTPAuth import HTTPBasicAuth
app = flask.Flask(__name__)
auth = HTTPBasicAuth()
@app.route("/", methods=["GET"])
def hello():
        who = flask.request.args.get("who", "World")
        return f"Hello {who}!\n"
@app.route('/rest-auth')
@auth.login_required
def get_response():
        return jsonify('You are authorized to see this message')
@auth.verify_password
def authenticate(username, password):
        if username and password:
                if username == 'test' and password == '2345':
                        return True
                else:
                        return False
        return False
if __name__ == "__main__":
    app.run(host="localhost", port=8080, debug=True)


ajorda

unread,
Feb 2, 2021, 3:59:57 AM2/2/21
to Google App Engine
Hello,

I have managed to reproduce your issue and solve it. I have noticed two points of improvement that I would like to address so that you can move forward with your application.

When you run a `pip install` command in your shell, it is only installing the package on your local machine unless you are working in a virtual environment (which is recommended) and uploading the "venv" folder (which shouldn't be done).
In order to install a dependency on GAE, you should use the "requirements.txt" file. This will allow GAE to be aware of the application's dependencies and install them by running `pip install -r requirements.txt`. This is explained in the documentation. In order to fix your issue I added the `flask_httpauth` library in the "requirements.txt" file.

Apart from that, I have noticed that the `flask_httpauth` is not properly imported in the code you provided. That causes your code to fail to execute even if the right dependency is installed. You can verify this by running `python main.py` in your local machine. In order to fix that issue I have changed the following line:

    from Flask_HTTPAuth import HTTPBasicAuth

By this one:

    from flask_httpauth import HTTPBasicAuth

After doing those changes I deployed to GAE and I was able to use your code to authenticate myself and properly access the `/rest-auth` endpoint.

Let us know if we can be of any further help.

Best regards,

Àlex J.

Marchello Lippi

unread,
Feb 10, 2021, 11:14:48 AM2/10/21
to Google App Engine
It worked, thanks!


вівторок, 2 лютого 2021 р. о 10:59:57 UTC+2 ajorda пише:
Reply all
Reply to author
Forward
0 new messages