Rahul Verma
Rahul Verma

Reputation: 3176

gunicorn ImportError: No module named flask

I have an anaconda virtual environment and I have already install flask in the virtual environment using pip when I run my app using

$ python app.py 

then it's working correctly, But when I run it with gunicorn I't showing that no module flask
app.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "Hello World"

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

wsgi.py

from app import app
if __name__ == "__main__":
    app.run()
 $ gunicorn --bind 0.0.0.0:8000 wsgi
[2019-07-23 12:39:24 +0000] [25612] [INFO] Starting gunicorn 19.9.0
[2019-07-23 12:39:24 +0000] [25612] [INFO] Listening at: http://0.0.0.0:8000 (25612)
[2019-07-23 12:39:24 +0000] [25612] [INFO] Using worker: sync
[2019-07-23 12:39:24 +0000] [25616] [INFO] Booting worker with pid: 25616
[2019-07-23 12:39:24 +0000] [25616] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/home/rahul/.local/lib/python2.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/home/rahul/.local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
    self.load_wsgi()
  File "/home/rahul/.local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/home/rahul/.local/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/home/rahul/.local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
    return self.load_wsgiapp()
  File "/home/rahul/.local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/home/rahul/.local/lib/python2.7/site-packages/gunicorn/util.py", line 350, in import_app
    __import__(module)
  File "/home/rahul/test_folder/wsgi.py", line 1, in <module>
    from app import app
  File "/home/rahul/test_folder/app.py", line 1, in <module>
    from flask import Flask
ImportError: No module named flask
[2019-07-23 12:39:24 +0000] [25616] [INFO] Worker exiting (pid: 25616)
[2019-07-23 12:39:24 +0000] [25612] [INFO] Shutting down: Master
[2019-07-23 12:39:24 +0000] [25612] [INFO] Reason: Worker failed to boot.

Upvotes: 7

Views: 4210

Answers (2)

Gitau Harrison
Gitau Harrison

Reputation: 3517

gunicorn may be installed in a global scope and not in your active virtual environment. A global scope installation may be:

$ sudo apt-get install gunicorn

What you need to do is first remove gunicorn from the global scope by running:

$ sudo apt remove gunicorn

The install it in your active virtual environment using pip3 (if you have Python3):

# Create your virtual environment
$ python -m venv venv

# Activate your virtual environment
$ source venv/bin/activate

# Install gunicorn
(venv)$ pip3 install gunicorn

Finally, you can run your flask application

Upvotes: 3

ferdina kusumah
ferdina kusumah

Reputation: 181

i think you forget to add app when run gunicorn

gunicorn --bind 0.0.0.0:8000 wsgi:app

it will run like this

→ gunicorn --bind 0.0.0.0:8000 wsgi:app
[2021-06-06 12:39:44 +0700] [54561] [INFO] Starting gunicorn 20.1.0
[2021-06-06 12:39:44 +0700] [54561] [INFO] Listening at: http://0.0.0.0:8000 (54561)
[2021-06-06 12:39:44 +0700] [54561] [INFO] Using worker: sync
[2021-06-06 12:39:44 +0700] [54563] [INFO] Booting worker with pid: 54563

and tested like this

→ curl http://localhost:8000
Hello World% 

Upvotes: 2

Related Questions