我无法在EC2 Micro实例的烧瓶应用程序中导入NumPy,sklearn等模块。
在/ home / ubuntu目录中,我有一个目录flaskapp
,其中包含:
flaskapp.py
flaskapp.wsgi
flaskapp.py
中的代码是:
import sys
import os
import shutil
import time
import traceback
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/predict')
def predict():
from sklearn.externals import joblib
import numpy as np
clf = joblib.load("flaskapp/models/saved_model.pkl")
print 'model loaded'
if clf:
try:
prediction = list(clf.predict(np.array([['0', '100', '164', '150', '1']])))
return jsonify({'prediction': prediction})
except Exception, e:
return "ERROR" + e
else:
print os.getcwd()
return 'no model here'
if __name__ == '__main__':
from sklearn.externals import joblib
import numpy as np
print "Imports Done"
app.run(DEBUG=True)
flaskapp.wsgi看起来像这样:
import sys
import site
site.addsitedir('/home/ubuntu/.local/lib/python2.7/site-packages')
sys.path.insert(0, '/var/www/html/flaskapp')
from flaskapp import app as application
根据以下问题: Numpy ImportError when deploying Flask App using mod_wsgi/Apache2
我修改了我的/etc/apache2/sites-enabled/000-default.conf
(以及网站也可用):
WSGIPythonPath /usr/local/lib/python2.7/site-packages/
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
WSGIDaemonProcess flaskapp threads=5
WSGIScriptAlias / /var/www/html/flaskapp/flaskapp.wsgi
<Directory flaskapp>
WSGIProcessGroup flaskapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
转到网址http://ec2-....compute-1.amazonaws.com/predict会抛出:
500内部服务器错误
服务器遇到内部错误但无法完成 你的申请。服务器过载或出现错误 申请。
错误日志显示以下内容:
[Thu Nov 02 22:15:21.936872 2017] [wsgi:error] [pid 11753:tid 140263731681024] [client 130.245.192.4:11592] [2017-11-02 22:15:21,936] ERROR in app: Exception on /predict [GET]
[Thu Nov 02 22:15:21.936900 2017] [wsgi:error] [pid 11753:tid 140263731681024] [client 130.245.192.4:11592] Traceback (most recent call last):
[Thu Nov 02 22:15:21.936903 2017] [wsgi:error] [pid 11753:tid 140263731681024] [client 130.245.192.4:11592] File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1982, in wsgi_app
[Thu Nov 02 22:15:21.936905 2017] [wsgi:error] [pid 11753:tid 140263731681024] [client 130.245.192.4:11592] response = self.full_dispatch_request()
[Thu Nov 02 22:15:21.936907 2017] [wsgi:error] [pid 11753:tid 140263731681024] [client 130.245.192.4:11592] File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1614, in full_dispatch_request
[Thu Nov 02 22:15:21.936909 2017] [wsgi:error] [pid 11753:tid 140263731681024] [client 130.245.192.4:11592] rv = self.handle_user_exception(e)
[Thu Nov 02 22:15:21.936911 2017] [wsgi:error] [pid 11753:tid 140263731681024] [client 130.245.192.4:11592] File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1517, in handle_user_exception
[Thu Nov 02 22:15:21.936913 2017] [wsgi:error] [pid 11753:tid 140263731681024] [client 130.245.192.4:11592] reraise(exc_type, exc_value, tb)
[Thu Nov 02 22:15:21.936915 2017] [wsgi:error] [pid 11753:tid 140263731681024] [client 130.245.192.4:11592] File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1612, in full_dispatch_request
[Thu Nov 02 22:15:21.936917 2017] [wsgi:error] [pid 11753:tid 140263731681024] [client 130.245.192.4:11592] rv = self.dispatch_request()
[Thu Nov 02 22:15:21.936918 2017] [wsgi:error] [pid 11753:tid 140263731681024] [client 130.245.192.4:11592] File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1598, in dispatch_request
[Thu Nov 02 22:15:21.936920 2017] [wsgi:error] [pid 11753:tid 140263731681024] [client 130.245.192.4:11592] return self.view_functions[rule.endpoint](**req.view_args)
[Thu Nov 02 22:15:21.936922 2017] [wsgi:error] [pid 11753:tid 140263731681024] [client 130.245.192.4:11592] File "/var/www/html/flaskapp/flaskapp.py", line 37, in predict
[Thu Nov 02 22:15:21.936924 2017] [wsgi:error] [pid 11753:tid 140263731681024] [client 130.245.192.4:11592] import numpy as np
[Thu Nov 02 22:15:21.936931 2017] [wsgi:error] [pid 11753:tid 140263731681024] [client 130.245.192.4:11592] ImportError: No module named numpy
答案 0 :(得分:1)
问题是apache2无法访问该目录。可悲的是,上面提到的问题中只有很少的评论,并且接受的答案并不正确。
如果有人帮助,请在此处添加解决方案:
sudo chown -R www-data:www-data /home
答案 1 :(得分:0)
sudo groupadd www-data
sudo usermod -a -G www-data ubuntu
sudo chown root:www-data /home
sudo chmod -R 775 /home
这应该可以运行