Report this

What is the reason for this report?

Flask and wsgi, ImportError: cannot import name app

Posted on January 1, 2015

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 112.64.71.131] Traceback (most recent call last):
[Thu Jan 01 01:35:21 2015] [error] [client 112.64.71.131]   File "/var/www/qianshan/qianshan.wsgi", line 7, in <module>
[Thu Jan 01 01:35:21 2015] [error] [client 112.64.71.131]     from qianshan import app as application
[Thu Jan 01 01:35:21 2015] [error] [client 112.64.71.131]  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
            </Directory>
            Alias /static /var/www/qianshan/qianshan/static
            <Directory /var/www/qianshan/qianshan/static/>
                    Order allow,deny
                    Allow from all
            </Directory>
            ErrorLog ${APACHE_LOG_DIR}/error.log
            LogLevel warn
            CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

wsgi

#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/qianshan/")

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__)

@app.route('/')
def index():
    block_list = getBlockList()
    website_dict = getWebsiteDict()
    return render_template('index.html', block_list=block_list, website_dict=website_dict)
...
...
if __name__ == '__main__':
app.run()

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.