我正在尝试使用Flask restful创建一个openshift应用程序。我可以按照openshift website中的说明设置我的应用。但除此之外,我想使用flask_restful。
所以我创建了一个virtualenv和pip安装了flask_restful。然后使用Flask_restful网站上的example复制示例代码并将其粘贴到flaskapp.py中(请参阅openshift example)。我还在 requirements.txt 中添加了 Flask-RESTful == 0.3.5 。
然后我能够运行python wsgi.py并在我的机器上本地成功运行。我能够得到我期待的结果。我有点期待当我将这些更改推送到openshift时,它会顺利运行。但它没有给我一个 500内部服务器错误消息。
为了给你一个想法,这里有一些我的文件
requirements.txt
Flask==0.10.1
Flask-RESTful==0.3.5:
flaskapp.py
from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class HelloWorld(Resource):
def get(self):
return {'hello': 'world'}
api.add_resource(HelloWorld, '/')
if __name__ == '__main__':
app.run(debug=True)
wsgi.py
#!/usr/bin/python
import os
#virtenv = os.environ['OPENSHIFT_PYTHON_DIR'] + '/virtenv/'
virtenv = os.path.join(os.environ.get('OPENSHIFT_PYTHON_DIR','.'), 'virtenv')
virtualenv = os.path.join(virtenv, 'bin/activate_this.py')
try:
execfile(virtualenv, dict(__file__=virtualenv))
except IOError:
pass
#
# IMPORTANT: Put any additional includes below this line. If placed above this
# line, it's possible required libraries won't be in your searchable path
#
from flaskapp import app as application
#
# Below for testing only
#
if __name__ == '__main__':
from wsgiref.simple_server import make_server
httpd = make_server('localhost', 8051, application)
# Wait for a single request, serve it and quit.
#httpd.handle_request()
httpd.serve_forever()
日志:
192.69.20.2 - - [30/Nov/2016:15:40:04 -0500] "GET / HTTP/1.1" 500 637 "-" "Wget/1.15 (linux-gnu)"
[Wed Nov 30 15:40:13 2016] [error] [client 127.5.221.1] mod_wsgi (pid=411796): Target WSGI script '/var/lib/openshift/583f10ac2d5271154f00001d/app-root/runtime/repo/wsgi.py' cannot be loaded as Python module.
[Wed Nov 30 15:40:13 2016] [error] [client 127.5.221.1] mod_wsgi (pid=411796): Exception occurred processing WSGI script '/var/lib/openshift/583f10ac2d5271154f00001d/app-root/runtime/repo/wsgi.py'.
[Wed Nov 30 15:40:13 2016] [error] [client 127.5.221.1] Traceback (most recent call last):
[Wed Nov 30 15:40:13 2016] [error] [client 127.5.221.1] File "/var/lib/openshift/583f10ac2d5271154f00001d/app-root/runtime/repo/wsgi.py", line 15, in <module>
[Wed Nov 30 15:40:13 2016] [error] [client 127.5.221.1] from myapp import app as application
[Wed Nov 30 15:40:13 2016] [error] [client 127.5.221.1] File "/var/lib/openshift/583f10ac2d5271154f00001d/app-root/runtime/repo/myapp.py", line 1, in <module>
[Wed Nov 30 15:40:13 2016] [error] [client 127.5.221.1] from flask import Flask
[Wed Nov 30 15:40:13 2016] [error] [client 127.5.221.1] ImportError: No module named flask
192.69.20.2 - - [30/Nov/2016:15:40:13 -0500] "GET / HTTP/1.1" 500 637 "-" "Wget/1.15 (linux-gnu)"
答案 0 :(得分:0)
终于明白了。我需要将flask_restful添加到setup.py
<强> setup.py 强>
from setuptools import setup
setup(name='YourAppName',
version='1.0',
description='OpenShift App',
author='Lance',
author_email='example@example.com',
url='http://www.python.org/sigs/distutils-sig/',
install_requires=['flask_restful'],
)