Issue
I'm codding a webserver to be hosted in an aws ec2 instance with Ubuntu, later I will link it to the frontend for POST and GET requisitions. I did the same steps as shown in href="https://www.youtube.com/watch?v=KgAtZ1LlNiQ" rel="nofollow noreferrer">Deploying a Simple Python Web Server for this project. The problem is, when I try to use the command gunicorn -w 3 objDetection:app
, I get the following error:
[2023-04-24 21:50:30 +0000] [4826] [INFO] Starting gunicorn 20.1.0
[2023-04-24 21:50:30 +0000] [4826] [INFO] Listening at: http://127.0.0.1:8000 (4826)
[2023-04-24 21:50:30 +0000] [4826] [INFO] Using worker: sync
[2023-04-24 21:50:30 +0000] [4827] [INFO] Booting worker with pid: 4827
[2023-04-24 21:50:30 +0000] [4827] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/gunicorn/arbiter.py", line 589, in spawn_worker
worker.init_process()
File "/usr/lib/python3/dist-packages/gunicorn/workers/base.py", line 134, in init_process
self.load_wsgi()
File "/usr/lib/python3/dist-packages/gunicorn/workers/base.py", line 146, in load_wsgi
self.wsgi = self.app.wsgi()
File "/usr/lib/python3/dist-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/usr/lib/python3/dist-packages/gunicorn/app/wsgiapp.py", line 58, in load
return self.load_wsgiapp()
File "/usr/lib/python3/dist-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
return util.import_app(self.app_uri)
File "/usr/lib/python3/dist-packages/gunicorn/util.py", line 384, in import_app
mod = importlib.import_module(module)
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/home/ubuntu/objDetection/__init__.py", line 1, in <module>
import object_detection
ModuleNotFoundError: No module named 'object_detection'
[2023-04-24 21:50:30 +0000] [4827] [INFO] Worker exiting (pid: 4827)
[2023-04-24 21:50:30 +0000] [4826] [INFO] Shutting down: Master
[2023-04-24 21:50:30 +0000] [4826] [INFO] Reason: Worker failed to boot.
to better understand the problem, here is the path of my project:
...
/home
|
/ubuntu
|
/objDetection --> My project folder, everything is here
|
__pycache__
__init__.py
coco.names
frozen_inference_graph.pb
index.html
object_detection.py --> error
ssd_mobilenet_v3_large_coco_2020_01_04.pbtxt
venv --> Virtual environment, used only to install gunicorn, everything else was installed normally (python3, pip, flask, nginx)
And also my init.py file:
import object_detection # this seems to be the problem
from flask import Flask, request, send_file, render_template
from flask_cors import CORS
# Define Flask and CORS
app = Flask(__name__)
CORS(app)
@app.route('/')
def webpage():
return render_template("index.html")
# Endpoint for receiving an image for processing photo
@app.route('/post-photo', methods=['POST'])
def handle_image():
# Retrieving the POST image formData from frontend
image = request.files.get('image')
# Return error if image wasn't on the formData
if image is None:
return 'Image not found in form', 400
# Saving image locally
image.save('./Images/my-photo.jpg')
# Processing image
object_detection.process_image('./Images/my-photo.jpg')
# Return received image message
return 'Image received successfully!', 200
# Endpoint for sending the processed photo
@app.route('/get-processed-photo', methods=['GET'])
def send_processed_photo():
processed_image_path = 'Images/processed-photo.jpg'
return send_file(processed_image_path, mimetype='image/jpg')
if __name__ == '__main__':
app.run(debug=True)
As far as I can understand, gunicorn can't find or doesn't recognize the import of object_detection.py.
Could a kind soul help me with this problem, I started developing this project recently and still don't know everything about nginx, gunicorn. Would be great to solve this problem and also learn from it if someone have the knowledge and can explain it.
I installed python3, pip, flask and nginx. Them made a virtual environment to install gunicorn in it since I saw in other topics about global paths of gunicorn. I also tried using wsgi file as shown in How to Deploy Flask with Gunicorn and Nginx (on Ubuntu), but got to nowhere.
I was expecting to execute the command gunicorn -w 3 objDetection:app
without any errors and get my webserver running.
PS:Sorry for the youtube links, it's my first time asking a question and coding this kind of stuff.
Solution
Not sure if I got it right, but as far as I could test it seems that you are not executing the gunicorn
command right.
First, activate the virtual environment with:
$ source venv/bin/activate
And then rename file __init__.py
to something like app.py
, and then run:
(venv)$ gunicorn -w 3 main:app
And also, if you only used venv
to install gunicorn
it is possible that you might get issues with other dependencies (such as flask
and flask_cors
). If that's the case, just install them and run the gunicorn
command again.
Hope that helps!
Answered By - mathlino Answer Checked By - Gilberto Lyons (WPSolving Admin)