0

I'm trying to create an openshift app using Flask restful. I was able to setup my app following the instructions in openshift website. But aside from that I want to use flask_restful.

So I created a virtualenv and pip installed flask_restful. Then using the example from the Flask_restful website, I copied the sample code and pasted it in the flaskapp.py (see openshift example). I also added Flask-RESTful==0.3.5 in the requirements.txt.

Then I was able to run python wsgi.py and it ran successfully locally in my machine. I was able to get the result that I'm expecting. I was kind of expecting that when I push these changes to openshift, it will run smoothly. But it didn't it gave me a 500 Internal Server Error message.

To give you an idea, here are some of my files

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()

Logs:

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)"
Lance
  • 2,774
  • 4
  • 37
  • 57

1 Answers1

0

Finally figured it out. I need to add flask_restful to 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'],
     )
Lance
  • 2,774
  • 4
  • 37
  • 57