Korba
Korba

Reputation: 435

ModuleNotFoundError: No module named 'flask_httpauth'

I am now using gunicorn to start a flask server. However, while doing so, I am getting a ModuleNotFoundError when trying to launch a flask server using

gunicorn src:app --config gunicorn_config.py

Flask Code in src/init.py

1 from flask import Flask
2 from flask_httpauth import HTTPBasicAuth
3 from werkzeug.middleware.proxy_fix import ProxyFix
4 
5 app = Flask(__name__)
6 app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)
7 auth = HTTPBasicAuth()

Error:

(venv)  backend #> gunicorn src:app --config gunicorn_config.py                                         
[2021-04-22 19:39:23 -0700] [4149] [INFO] Starting gunicorn 20.0.4
[2021-04-22 19:39:23 -0700] [4149] [INFO] Listening at: http://0.0.0.0:8443 (4149)
[2021-04-22 19:39:23 -0700] [4149] [INFO] Using worker: sync
[2021-04-22 19:39:23 -0700] [4150] [INFO] Booting worker with pid: 4150
[2021-04-22 19:39:23 -0700] [4151] [INFO] Booting worker with pid: 4151
[2021-04-22 19:39:23 -0700] [4152] [INFO] Booting worker with pid: 4152
[2021-04-22 19:39:23 -0700] [4150] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py", line 119, in init_process
    self.load_wsgi()
  File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
    return self.load_wsgiapp()
  File "/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python3.9/site-packages/gunicorn/util.py", line 358, in import_app
    mod = importlib.import_module(module)
  File "/usr/local/Cellar/[email protected]/3.9.1_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "<PWD>backend/src/__init__.py", line 2, in <module>  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< HERE <<<<<
    from flask_httpauth import HTTPBasicAuth
ModuleNotFoundError: No module named 'flask_httpauth'

Verified that package installed in a virtual environment.

(venv) backend #> pip list | grep -i flask-httpauth
Flask-HTTPAuth        4.0.0
(venv) backend #> which pip
<PWD>/venv/bin/pip
(venv) backend #> which python
<PWD>/venv/bin/python
(venv) backend #> python -V
Python 3.9.1

For what it's worth, I was able to run it earlier and launch successfully. However it stopped working this afternoon.

Upvotes: 1

Views: 1398

Answers (1)

Henshal B
Henshal B

Reputation: 2002

Accoring to Flask standards, you should keep app instance not in __init__ but in any custom module. If you place it in __init__, you must use factory layout. Also, gunicorn points not to module, but to a file. In your case since you call gunicorn to app, you should place app instance in app.py file in src folder.

Upvotes: 1

Related Questions