python - 在 OpenShift 的书籍示例中使用 Python 3.3

标签 python python-3.x openshift

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/

相关文章:

node.js - Openshift - 503 服务在我访问应用程序时暂时不可用

java - Openshift 访问 context.xml

python - 有没有办法强制将数据导入 Odoo 9 或 10 中的只读字段?

python - 为什么@staticmethod 没有跨类保留,而@classmethod 是?

Python 页面倒计时打印 (len(elem_href1-(number)))

python - 如何在 Python 3 中正确包装字典?

python - python中的 Spark 单词计数程序错误

python-3.x - odoo 从代码向用户发送消息(通知)

python - 是什么导致 [*a] 过度分配?

docker - 我可以查询OpenShift Docker存储库是否给定镜像_exists_而不拉出它吗?