ModuleNotFoundError: No module named ‘main’

google-app-engine

I have tried to deploy flask app on Google App Engine Flexible environment. While deploying the app, I got following error.

raceback (most recent call last):
  File "/env/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/env/lib/python3.7/site-packages/gunicorn/workers/base.py", line 119, in init_process      
    self.load_wsgi()
  File "/env/lib/python3.7/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/env/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/env/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
    return self.load_wsgiapp()
  File "/env/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp        
    return util.import_app(self.app_uri)
  File "/env/lib/python3.7/site-packages/gunicorn/util.py", line 358, in import_app
    mod = importlib.import_module(module)
  File "/opt/python3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'main'
[2021-03-06 14:39:52 +0000] [8] [INFO] Worker exiting (pid: 8)
[2021-03-06 14:39:52 +0000] [1] [INFO] Shutting down: Master
[2021-03-06 14:39:52 +0000] [1] [INFO] Reason: Worker failed to boot.

Here is the requirements.txt file.

backcall==0.2.0
click==7.1.2
cloudpickle==1.6.0
colorama==0.4.4
cycler==0.10.0
decorator==4.4.2
Flask==1.1.2
Flask-Cors==3.0.10
ipython==7.21.0
ipython-genutils==0.2.0
itsdangerous==1.1.0
jedi==0.18.0
Jinja2==2.11.3
joblib==1.0.1
kiwisolver==1.3.1
llvmlite==0.35.0
MarkupSafe==1.1.1
matplotlib==3.2.0
numba==0.52.0
numpy==1.20.1
pandas==1.2.3
parso==0.8.1
pickle-mixin==1.0.2
pickleshare==0.7.5
Pillow==8.1.1
prompt-toolkit==3.0.16
Pygments==2.8.0
pyparsing==2.4.7
PyQt5==5.15.3
PyQt5-Qt==5.15.2
PyQt5-sip==12.8.1
python-dateutil==2.8.1
pytz==2021.1
scikit-learn==0.24.1
scipy==1.6.1
shap==0.39.0
six==1.15.0
sklearn==0.0
slicer==0.0.7
threadpoolctl==2.1.0
tqdm==4.58.0
traitlets==5.0.5
wcwidth==0.2.5
Werkzeug==1.0.1
gunicorn==20.0.4

and app.yaml file

runtime: python
env: flex
entrypoint: gunicorn -b:$PORT main:app

runtime_config:
  python_version: 3.7

automatic_scaling:
  min_num_instances: 1
  max_num_instances: 2

Im also attaching my app.py file bellow.

from flask import Flask, request, render_template, send_from_directory
from flask_cors import CORS
from ml import preprocess, predict

app = Flask(__name__)
CORS(app)

@app.route('/assets/<path:path>')
def send_js(path):
    return send_from_directory('static/assets', path)


@app.route('/', methods = ['POST', 'GET'])
def index():
    if request.method == 'POST':
        prediction_dict = preprocess.transform(request.data)
        
        scaled = preprocess.scaler([list(prediction_dict.values())])

        return preprocess.dump({
            'message': predict.do_predict(scaled).tolist()[0], 
            'review': predict.do_cause(prediction_dict)
        })
    else:
        return render_template('index.html')

@app.route('/status', methods = ['GET']) 
def status(): 
    return preprocess.dump({'accuracy': 95.02}); 

if __name__ == '__main__':
    # This is used when running locally. Gunicorn is used to run the
    # application on Google App Engine. See entrypoint in app.yaml.
    app.run(host='127.0.0.1', port=8080, debug=True)

Best Answer

This line says to look for the variable named app in the module named main.py:
entrypoint: gunicorn -b:$PORT main:app

You could either rename your app.py to main.py or update this line to:
entrypoint: gunicorn -b:$PORT app:app

See also: https://docs.gunicorn.org/en/stable/run.html

Related Topic