Flask and wsgi, ImportError: cannot import name app


大家好,做了一个flask的小应用,配置在digital ocean上,按照digital ocean的配置说明配置后 链接描述 ,访问站点时报500错误。看了下apache的日志,错误原因如下,还请帮忙看看。

日志报错:


 [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

项目结构:


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

虚拟主机配置


 <VirtualHost *:80>
        ServerName qianshan.co
        ServerAdmin [email protected]
        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()

python flask wsgi digitalocean

cccp127 11 years, 10 months ago

我之前在do上部署Flask应用 用的是gunicorn。详情可以看我的博客: http://defshine.github.io/deploy-flask-app-on-do.html

我试着在本地机器上,按照楼主的方式部署了一下,恰巧也遇到这个错误。
最终发现问题时在wsgi的那个文件中
推荐 楼主检查
sys.path.insert(0,"/var/www/qianshan/")
这个路径/var/www/qianshan/ 是项目的路径吗?
比如的项目放在 /home/xin/workspace/python/flaskapp这里那我的配置就是:
import sys
import logging

logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/home/xin/workspace/python/flaskapp/")

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

夜幕下的哈里发 answered 11 years, 10 months ago

Your Answer