Flask and wsgi, ImportError: cannot import name app

January 1, 2015 4.4k views

Hi I'm working on my first flask project, very simple one.

I'm deploying my site on Digital Ocean, ubuntu server. And I complied with the instruction hereHow To Deploy a Flask Application on an Ubuntu VPS

When I tried to open the site in browser, it came with 500 error. Then I looked up the apache error log to find the following sentences.

Error log

[Thu Jan 01 01:35:21 2015] [error] [client] Traceback (most recent call last):
[Thu Jan 01 01:35:21 2015] [error] [client]   File "/var/www/qianshan/qianshan.wsgi", line 7, in <module>
[Thu Jan 01 01:35:21 2015] [error] [client]     from qianshan import app as application
[Thu Jan 01 01:35:21 2015] [error] [client]  ImportError: cannot import name app

Tree structure of the project

├── qianshan
│   ├── config.ini
│   ├── __init__.py
│   ├── static
│   ├── templates
│   └── venv
└── qianshan.wsgi

Virtual Host configuration

<VirtualHost *:80>
            ServerName qianshan.co
            ServerAdmin spark@qianshan.co
            WSGIScriptAlias / /var/www/qianshan/qianshan.wsgi
            <Directory /var/www/qianshan/qianshan/>
                    Order allow,deny
                    Allow from all
            Alias /static /var/www/qianshan/qianshan/static
            <Directory /var/www/qianshan/qianshan/static/>
                    Order allow,deny
                    Allow from all
            ErrorLog ${APACHE_LOG_DIR}/error.log
            LogLevel warn
            CustomLog ${APACHE_LOG_DIR}/access.log combined


import sys
import logging

from qianshan import app as application
application.secret_key = 'Add your secret key'

init.py file

# Filename: __init__.py
# encoding: utf-8

import ConfigParser
import codecs
from flask import Flask
from flask import render_template

app = Flask(__name__)

def index():
    block_list = getBlockList()
    website_dict = getWebsiteDict()
    return render_template('index.html', block_list=block_list, website_dict=website_dict)
if __name__ == '__main__':

1 Answer

That error generally means that Flask was not installed correctly. Did you receive any error messages when you installed Flask into the virtualenv?

cd /var/www/qianshan/qianshan/
source venv/bin/activate 
sudo pip install Flask 

Does the Flask app run when you call it directly from inside the virtualenv using sudo python __init__.py ?

If you saw an error similar to this:

    x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c markupsafe/_speedups.c -o build/temp.linux-x86_64-2.7/markupsafe/_speedups.o
    markupsafe/_speedups.c:12:20: fatal error: Python.h: No such file or directory
     #include <Python.h>
    compilation terminated.
    WARNING: The C extension could not be compiled, speedups are not enabled.
    Failure information, if any, is above.
    Retrying the build without the C extension now.

    WARNING: The C extension could not be compiled, speedups are not enabled.
    Plain-Python installation succeeded.

when trying to install Flask, that means you need to install the development headers for Python in order to compile the C extension. This can be done with:

sudo apt-get install python-dev
Have another answer? Share your knowledge.