search

Home  >  Q&A  >  body text

python - 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 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()
伊谢尔伦伊谢尔伦2807 days ago913

reply all(3)I'll reply

  • 黄舟

    黄舟2017-04-17 14:01:49

    I used gunicorn to deploy Flask applications on do. For details, please see my blog: http://defshine.github.io/deploy-flask-app-on-do.html

    I tried to deploy it on the local machine according to the original poster's method, and I happened to encounter this error.
    When the problem was finally discovered, it was in the wsgi file
    Recommended to be checked by the original poster
    sys.path.insert(0,"/var/www/qianshan/")
    Is this path /var/www/qianshan/ the path of the project?
    For example, if the project is placed in /home/xin/workspace/python/flaskapp, then my configuration is:
    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'

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 14:01:49

    Insert init.py
    if name == 'main':
    app.run()
    Delete these two sentences.

    Create a new file, runsvr,py, with the following content:
    from init import app
    if name == 'main':
    app.run()

    During normal debugging
    Use python runsvr.py to run

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 14:01:49

    Problem solved, http://segmentfault.com/q/1010000002467850

    Permission issue

    reply
    0
  • Cancelreply