OpenShift 最近出版了一本书,“Getting Started with OpenShift”。对于刚起步的人来说,这是一个很好的指南。
在第 3 章中,他们展示了如何修改模板应用程序以使用 Python 2.7 和 Flask。我们的要求是 Python 3.3。
在第19页,对wsgi.py的修改之一是:execfile(virtualenv, dict(file=virtualenv))。 execfile 在 3.x 中被取消了。 StackOverflow 中有关于如何翻译的示例,但我不清楚如何将这些应用到这种情况。
有没有人对这个问题有任何见解?
最佳答案
如 this question 中所示, 你可以替换行
execfile(virtualenv, dict(__file__=virtualenv))
通过
exec(compile(open(virtualenv, 'rb').read(), virtualenv, 'exec'), dict(__file__=virtualenv))
在我看来,最好将其分解成几个更简单的部分。我们还应该使用上下文处理程序来处理文件::
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)
以这种方式分解它也会使调试更容易(实际上:可能)。我还没有测试过这个,但它应该可以工作。
关于python - 在 OpenShift 的书籍示例中使用 Python 3.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23418735/