Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Python 3.3 in OpenShift's book example

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?

like image 591
user3501978 Avatar asked Oct 01 '22 21:10

user3501978


1 Answers

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.

like image 152
Caleb Hattingh Avatar answered Oct 05 '22 23:10

Caleb Hattingh