Flask and wsgi, ImportError: cannot import name app

  • Posted on January 1, 2015
  • teannnnAsked by teannnn

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

Problem 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
│   ├──
│   ├── static
│   ├── templates
│   └── venv
└── qianshan.wsgi

Virtual Host configuration

<VirtualHost *:80>
            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' file

# Filename:
# 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__':

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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 ?

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