Flask Deployment on Ubuntu 12.04 - 500 Error, logs: ImportError cannot import name Flask

July 9, 2014 2.7k views

I've followed this tutorial pretty much to the letter:


When I'm in the virtualenv, and running the Flask app on the localhost, I am able to:

curl localhost:5000

and receive a 200 http response "hello world"

But when I try to access the site from my browser, I get the following error log.

root@barchief:/var/www/flask# tail /var/log/apache2/error.log 
[Wed Jul 09 09:34:57 2014] [notice] caught SIGTERM, shutting down 
[Wed Jul 09 09:34:58 2014] [notice] Apache/2.2.22 (Ubuntu) mod_wsgi/3.3 Python/2.7.3 configured -- resuming normal operations 
[Wed Jul 09 09:35:02 2014] [error] [client] mod_wsgi (pid=10918): Target WSGI script '/var/www/flask/flask.wsgi' cannot be loaded as Python module. 
[Wed Jul 09 09:35:02 2014] [error] [client] mod_wsgi (pid=10918): Exception occurred processing WSGI script '/var/www/flask/flask.wsgi'. 
[Wed Jul 09 09:35:02 2014] [error] [client] Traceback (most recent call last): 
[Wed Jul 09 09:35:02 2014] [error] [client] File "/var/www/flask/flask.wsgi", line 10, in <module> 
[Wed Jul 09 09:35:02 2014] [error] [client] from flask import app as application 
[Wed Jul 09 09:35:02 2014] [error] [client] File "/var/www/flask/flask/__init__.py", line 1, in <module> 
[Wed Jul 09 09:35:02 2014] [error] [client] from flask import Flask 
[Wed Jul 09 09:35:02 2014] [error] [client] ImportError: cannot import name Flask

I'm getting an ImportError for Flask, but flask has to be installed because I'm able to access it from localhost. What should I do?

1 Answer

Did you install Flask inside a virualenv? If so, you need to activate it in your .wsgi file At the top, before Flask is imported, add something like:

activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

Check out the the Flask documentation on the topic.

Have another answer? Share your knowledge.