Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error from beanstalk when trying to deploy flask app: "no module named flask"

I've been trying to figure out this problem for a while but can't figure it out. My app structure is like this:

myapp  
    -application.py  
    -myapp  
       -sample.css  
       -sample.js  
       -blueprints.py  
       -__init__.py  
       -__init__.pyc  
       -templates  
         -base.jinja2  
   -node_modules  
   -package.json  
   -requirements.txt  
   -static  
   -venv  
   -webpack.config.js  

I have python 2.7 environment for beanstalk and the same for my virtual environment. I have all the needed packages in pip list and requirements.txt. My WSGI path in the yml file from eb config is set to /myapp/application.py. The exact error I get from eb logs is:

mod_wsgi (pid=2330): Target WSGI script '/opt/python/current/app/myapp/application.py' cannot be loaded as Python module.
mod_wsgi (pid=2330): Exception occurred processing WSGI script '/opt/python/current/app/myapp/application.py'.
"File "/opt/python/current/app/cloud-dev/application.py", line 3, in <module>
from flask import render_template
ImportError: No module named flask"

I keep getting a 500 error when going to the site link. Help is much appreciated!

like image 568
jChua Avatar asked Sep 21 '15 20:09

jChua


People also ask

How do you deploy a flask app to Elastic Beanstalk?

To deploy a Flask app to AWS Elastic Beanstalk, you need to do the following in order: Create your Flask app & provide Python dependencies: This will be your code that will be deployed to AWS Elastic Beanstalk along with requirements. txt. Zip your app: Zip all of your source code and dependencies into a single file.

When should you not use Beanstalk?

Elastic Beanstalk isn't great if you need a lot of environment variables. The simple reason is that Elastic Beanstalk has a hard limit of 4KB to store all key-value pairs. The environment had accumulated 74 environment variables — a few of them had exceedingly verbose names.


1 Answers

I encountered the exact same error. What helped for me is renaming the Flask object that you run to 'application':

from flask import Flask

application = Flask(__name__)

# run the app.
if __name__ == "__main__":
    application.run()

From the Amazon EB Docs:

Using application.py as the filename and providing a callable application object (the Flask object, in this case) allows AWS Elastic Beanstalk to easily find your application's code.

like image 108
Daxez Avatar answered Sep 29 '22 09:09

Daxez