尝试使用apache2部署一个简单的烧瓶应用程序时遇到问题。网站结构如下:
flaskapp/
├── assets
├── conf.d
├── config
└── __init__.py # Where the configuration is
├── deploy
├── modulos
├── requirements
├── templates
└── vistas
每当我尝试加载页面时,都会收到以下错误:
[Mon Oct 05 18:40:27.875240 2015] [:error] [pid 20674:tid 140375715673856] [remote 10.0.2.2:10690] mod_wsgi (pid=20674): Target WSGI script '/var/www/flaskapp/application.wsgi' cannot be loaded as Python module.
[Mon Oct 05 18:40:27.875565 2015] [:error] [pid 20674:tid 140375715673856] [remote 10.0.2.2:10690] mod_wsgi (pid=20674): Exception occurred processing WSGI script '/var/www/flaskapp/application.wsgi'.
[Mon Oct 05 18:40:27.875723 2015] [:error] [pid 20674:tid 140375715673856] [remote 10.0.2.2:10690] Traceback (most recent call last):
[Mon Oct 05 18:40:27.875886 2015] [:error] [pid 20674:tid 140375715673856] [remote 10.0.2.2:10690] File "/var/www/flaskapp/application.wsgi", line 12, in <module>
[Mon Oct 05 18:40:27.876195 2015] [:error] [pid 20674:tid 140375715673856] [remote 10.0.2.2:10690] import vistas
[Mon Oct 05 18:40:27.876358 2015] [:error] [pid 20674:tid 140375715673856] [remote 10.0.2.2:10690] File "/var/www/flaskapp/vistas/__init__.py", line 5, in <module>
[Mon Oct 05 18:40:27.876811 2015] [:error] [pid 20674:tid 140375715673856] [remote 10.0.2.2:10690] from .index import blueprint as inicio
[Mon Oct 05 18:40:27.876959 2015] [:error] [pid 20674:tid 140375715673856] [remote 10.0.2.2:10690] File "/var/www/flaskapp/vistas/index.py", line 4, in <module>
[Mon Oct 05 18:40:27.877477 2015] [:error] [pid 20674:tid 140375715673856] [remote 10.0.2.2:10690] from modulos import api
[Mon Oct 05 18:40:27.877623 2015] [:error] [pid 20674:tid 140375715673856] [remote 10.0.2.2:10690] File "/var/www/flaskapp/modulos/__init__.py", line 4, in <module>
[Mon Oct 05 18:40:27.877965 2015] [:error] [pid 20674:tid 140375715673856] [remote 10.0.2.2:10690] from config import conf
[Mon Oct 05 18:40:27.878137 2015] [:error] [pid 20674:tid 140375715673856] [remote 10.0.2.2:10690] ImportError: cannot import name conf
做了一些调试我发现了以下情况。当我在虚拟机中导入配置时:
>>> import config
>>> config
<module 'config' from '/var/www/flaskapp/config'>
当我在本地机器上这样做时:
>>> import config
>>> config
<module 'config' from '/etc/flaskapp/config/__init__.py'>
我做错了什么?这是我的apache配置文件:
<VirtualHost *:80>
ServerName testapp.com
ServerAlias flask.local
WSGIDaemonProcess flaskapp display-name=www-data user=www-data \
group=www-data processes=1 threads=5 \
home=/var/www/flaskapp \
python-path=/var/www/flaskapp:/opt/env_flaskapp/lib/python2.7/site-packages
WSGIProcessGroup flasskapp
WSGIScriptAlias / /var/www/flaskapp/application.wsgi
<Directory /var/www/flaskapp>
Require all granted
</Directory>
</VirtualHost>
更新:此处的__init__.py
除外,显示失败的地方
# -*- coding: utf-8 -*-
import os, yaml, datetime, locale
from flask import Flask
from flask.ext.cache import Cache
from flask.ext.bcrypt import Bcrypt
from flask_mail import Mail
from hashids import Hashids
archivo = open('/opt/carrito/conf.d/config.yaml', 'r')
conf = yaml.load(archivo)
STATIC_DIR = '%s/%s' % (conf['dirs']['full_path'],
conf['dirs']['static_dir'])
TEMPLATE_DIR = '%s/%s' % (conf['dirs']['full_path'],
conf['dirs']['template_dir'])
app = Flask(__name__, static_folder=STATIC_DIR,
template_folder=TEMPLATE_DIR)