5

OpenShift recently published a book, "Getting Started with OpenShift". It is a good guide for someone just starting out.

In Chapter 3 they show how to modify a template application to use Python 2.7 and Flask. Our requirement is for Python 3.3.

On Page 19, one of the modifications to wsgi.py is: execfile(virtualenv, dict(file=virtualenv)). execfile was done away with in 3.x. There are examples in StackOverflow on how to translate but it is not clear to me how to apply those to this case.

Does anyone have any insight into this issue?

user3501978
  • 63
  • 1
  • 5
  • 1
    I haven't read the new docs. It still sounds like they are not complete. There are actually two entry points to start your python app. Wsgi.py and app.py. Use wsgi.py if you are using OS 's apache server. app.py if you want to use your own server (cherrypy, waitress, simple server). I can give you start up code tomorrow when I'm next to my computer. – fat fantasma May 02 '14 at 01:12
  • The user is actually referring to this book https://www.openshift.com/blogs/announcing-a-new-book-getting-started-with-openshift-a-guide-for-impatient-beginners which does not aim to be complete – TheSteve0 May 02 '14 at 02:22
  • It looks like your exact question was answered below. If you still help actually getting everything to work let me know. – fat fantasma May 02 '14 at 14:18

2 Answers2

4

As indicated in this question, you can replace the line

execfile(virtualenv, dict(__file__=virtualenv))

by

exec(compile(open(virtualenv, 'rb').read(), virtualenv, 'exec'), dict(__file__=virtualenv))

In my opinion, it would be better to break this up into a few simpler pieces. Also we should use a context handler for the file handling::

with open(virtualenv, 'rb') as exec_file:
    file_contents = exec_file.read()
compiled_code = compile(file_contents, virtualenv, 'exec')
exec_namespace = dict(__file__=virtualenv)
exec(compiled_code, exec_namespace)

Breaking it up in this way will also make debugging easier (actually: possible). I haven't tested this but it should work.

Community
  • 1
  • 1
Caleb Hattingh
  • 9,005
  • 2
  • 31
  • 44
  • 2
    Many thanks to cjrh for his answers and to fantasma for his offer of additional help. Both answers worked well. It is now clear how to integrate Flask for OpenShift using Python v3.3. Code for this result can be downloaded from http://informationanthology.net/literature.zip. Anyone is welcome to partake. – user3501978 May 05 '14 at 20:00
0

If you are facing issues with virtualenv settings on wsgi.py file in Python3 I have solved just deleting it.

This is my wsgi.py file and it's working

#!/usr/bin/python

from flaskapp import app as application

if __name__ == '__main__':
    from wsgiref.simple_server import make_server
    httpd = make_server('0.0.0.0', 5000, application)
    # Wait for a single request, serve it and quit.
    #httpd.handle_request()
    httpd.serve_forever()
Ricardo
  • 7,921
  • 14
  • 64
  • 111