AWS AMI上的Opencv / Flask App

时间:2016-09-20 10:04:51

标签: python-2.7 opencv amazon-ec2 flask

我正在尝试在AWS AMI上运行一个应该使用OpenCV库的简单应用程序。我创建了一个虚拟机,并安装了OpenCV。对于安装,我遵循本教程http://www.pyimagesearch.com/2015/06/22/install-opencv-3-0-and-python-2-7-on-ubuntu/,似乎也没问题,因为我能够运行该示例。当我尝试运行我的Flask应用程序时出现问题,因为我收到以下错误:

[Tue Sep 20 09:58:14.117753 2016] [:error] [pid 15068] [remote 77.246.17.229:18121] Traceback (most recent call last):
[Tue Sep 20 09:58:14.117778 2016] [:error] [pid 15068] [remote 77.246.17.229:18121]   File "/var/www/flaskapp/app.wsgi", line 25, in <module>
[Tue Sep 20 09:58:14.117822 2016] [:error] [pid 15068] [remote 77.246.17.229:18121]     from app import app as application
[Tue Sep 20 09:58:14.117831 2016] [:error] [pid 15068] [remote 77.246.17.229:18121]   File "/var/www/flaskapp/app.py", line 13, in <module>
[Tue Sep 20 09:58:14.117878 2016] [:error] [pid 15068] [remote 77.246.17.229:18121]     from flask import (Flask, abort, flash, g, jsonify, make_response, render_template, request, session, url_for)
[Tue Sep 20 09:58:14.117887 2016] [:error] [pid 15068] [remote 77.246.17.229:18121]   File "/var/www/flaskapp/env/lib/python2.7/site-packages/flask/__init__.py", line 19, in <module>
[Tue Sep 20 09:58:14.117916 2016] [:error] [pid 15068] [remote 77.246.17.229:18121]     from jinja2 import Markup, escape
[Tue Sep 20 09:58:14.117923 2016] [:error] [pid 15068] [remote 77.246.17.229:18121]   File "/var/www/flaskapp/env/lib/python2.7/site-packages/jinja2/__init__.py", line 33, in <module>
[Tue Sep 20 09:58:14.117951 2016] [:error] [pid 15068] [remote 77.246.17.229:18121]     from jinja2.environment import Environment, Template
[Tue Sep 20 09:58:14.117965 2016] [:error] [pid 15068] [remote 77.246.17.229:18121]   File "/var/www/flaskapp/env/lib/python2.7/site-packages/jinja2/environment.py", line 13, in <module>
[Tue Sep 20 09:58:14.118148 2016] [:error] [pid 15068] [remote 77.246.17.229:18121]     from jinja2 import nodes
[Tue Sep 20 09:58:14.118157 2016] [:error] [pid 15068] [remote 77.246.17.229:18121]   File "/var/www/flaskapp/env/lib/python2.7/site-packages/jinja2/nodes.py", line 19, in <module>
[Tue Sep 20 09:58:14.118287 2016] [:error] [pid 15068] [remote 77.246.17.229:18121]     from jinja2.utils import Markup
[Tue Sep 20 09:58:14.118296 2016] [:error] [pid 15068] [remote 77.246.17.229:18121]   File "/var/www/flaskapp/env/lib/python2.7/site-packages/jinja2/utils.py", line 531, in <module>
[Tue Sep 20 09:58:14.118418 2016] [:error] [pid 15068] [remote 77.246.17.229:18121]     from markupsafe import Markup, escape, soft_unicode
[Tue Sep 20 09:58:14.118436 2016] [:error] [pid 15068] [remote 77.246.17.229:18121] ImportError: No module named markupsafe

但是我使用命令pip freeze检查了虚拟机上所有已安装的模块,我得到以下内容:

click==6.6
Flask==0.11.1
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
numpy==1.11.1
Werkzeug==0.11.11

因此MarkupSafe已安装并更新了最新版本。

所以开始认为问题可能出在我的.wgsi文件或httpd.conf中 app.wsgi:

import sys
import site
import os

# Add virtualenv site packages
site.addsitedir(os.path.join(os.path.dirname(__file__), '/var/www/flaskapp/env/lib/python2.7/site-packages'))

#activate_this = '/var/www/flaskapp/env/bin/activate_this.py'
#execfile(activate_this, dict(__file__=activate_this))

# Path of execution
sys.path.insert(0,'/var/www/flaskapp')

#import the app
from app import app as application

这个是httpd.conf: 听80

<VirtualHost *>
        ServerName ec2...
        WSGIDaemonProcess app threads=5 home=/var/www/flaskapp/ python-path=/var/www/flaskapp/env/lib/python2.7/site-packages threads=1

        DocumentRoot /var/www/flaskapp

        WSGIScriptAlias / /var/www/flaskapp/app.wsgi
        <Directory /var/www/flaskapp>
                WSGIProcessGroup app
                WSGIApplicationGroup %{GLOBAL}
                Order deny,allow
                Allow from all
        </Directory>
</VirtualHost>

但我无法弄清问题。我知道为什么会出现这个错误吗?

如果我尝试将我的app.wgsi引用到lib64而不仅仅是lib,则错误变为:ImportError:numpy.core.multiarray无法导入。我检查了lib和lib64中存在的不同的site-packages,我有这个配置:

lib: click - easy_install.pyc - itsdangerous.py - markerlib - setuptools - click-6.6-py2.7.egg-info - flask - itsdangerous.pyc - pip - setuptools-12.0.5.dist-info - cv2.so - Flask- 0.11.1.dist-info - jinja2 - pip-6.0.8.dist-info - werkzeug - easy_install.py - itsdangerous-0.24-py2.7.egg-info - Jinja2-2.8.dist-info - pkg_resources - Werkzeug- 0.11.11.dist-信息

在lib64中我有: markupsafe MarkupSafe-0.23-py2.7.egg-info numpy numpy-1.11.1-py2.7.egg-info

似乎mod_wsgi是为python 2.6.9配置的,我使用的是python 2.7。这可能是问题吗?

1 个答案:

答案 0 :(得分:0)

你不能为Python 2.6编译mod_wsgi,并尝试使用Python 2.7强制它使用Python虚拟环境。您需要卸载mod_wsgi并从二进制包或源代码重新安装它,但是它是为Python 2.7编译的。它不是更改使用的Python版本的配置选项。必须编译它才能开始使用正确的Python版本。