python - Flask and wsgi, ImportError: cannot import name app
伊谢尔伦
伊谢尔伦 2017-04-17 13:59:49
0
3
880

大家好,做了一个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()
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

Antworte allen(3)
黄舟

我之前在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'

大家讲道理

把init.py中
if name == 'main':
app.run()
这两句删除.

另新建一个文件runsvr,py, 内容如下:
from init import app
if name == 'main':
app.run()

平时调试的时候
用python runsvr.py来运行

洪涛

问题解决了,http://segmentfault.com/q/1010000002467850

权限问题

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!