让Flask使用Python3(Apache / mod_wsgi) [英] Getting Flask to use Python3 (Apache/mod_wsgi)

查看:406
本文介绍了让Flask使用Python3(Apache / mod_wsgi)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在Ubuntu 14.04上,使用Apache 2.4。我已经安装了mod_wsgi。



我创建了一个〜/ web / piFlask / venv / 安装了virtualenv的Python2。



然而,我想让我的flaskapp导入一个我写的Python3.x模块。



我需要做些什么才能做到这一点?



我试着创建一个〜/ web / piFlask / venv3 / 并修改〜/ web / piFlask / piFlask.wsgi

  import os,sys 

PROJECT_DIR ='/ home / pi / web / piFlask'

activate_this = os.path.join(PROJECT_DIR,'venv3 / bin','activate_this.py')
execfile(activate_this,dict(__ file __ = activate_this))
sys.path.insert(0,PROJECT_DIR)

from piFlask import app as application

application.debug = True

但是我没有认为这是足够的。这个.wsgi实际上是一个Python文件,它将被mod_wsgi执行,我确定会使用Py2.x解释器来执行。



我正确地理解,mod_wsgi启动系统Python来执行这个.wsgi,它会依次启动我的〜/ web / piFlask / venv / 解释器实际上处理请求。

我想我可以说服mod_wsgi使用系统Python3或我自己的venv3 / ...通过设置 WSGIPythonPath / home / pi / web / piFlask / venv3 / lib / python3.4 / site-packages 在/etc/apache2/mods-available/wsgi.conf



但是我在某处发现了一个指令,说你必须为Py3编译mod_wsgi,而这个问题的底部很快就会出现。

>

正确的话,mod_wsgi需要为特定的Python版本进行编译,因为它从来不会执行python可执行文件。相反,Python库链接到mod_wsgi。

最终的结果是,您不能在使用Python 2解释器运行的应用程序中混合Python 3代码。



您必须考虑在Python 2和Python 3下运行您的代码,然后您可以选择要使用的代码,并使用为您选择使用的版本编译的mod_wsgi版本。


I've got a basic "hello world" Flask app running.

I'm on Ubuntu 14.04, using Apache 2.4. I've installed mod_wsgi.

I've created a ~/web/piFlask/venv/ to hold a virtualenv-created Python2 with flask installed.

However, I wish to have my flaskapp import a Python3.x module I have written.

What do I need to do to make this happen?

I tried creating a ~/web/piFlask/venv3/ and modifying ~/web/piFlask/piFlask.wsgi:

import os, sys

PROJECT_DIR = '/home/pi/web/piFlask'

activate_this = os.path.join(PROJECT_DIR, 'venv3/bin', 'activate_this.py')
execfile(activate_this, dict(__file__=activate_this))
sys.path.insert(0, PROJECT_DIR)

from piFlask import app as application

application.debug = True

But I don't think this is sufficient. This .wsgi is in fact a Python file that will get executed by mod_wsgi, which I'm sure will use a Py2.x interpreter to do the execution.

So if I'm understanding correctly, mod_wsgi fires up the system Python in order to execute this .wsgi, which will in turn fire up my ~/web/piFlask/venv/ interpreter to actually process the request.

I think I could persuade mod_wsgi to use either a system Python3 or my own venv3/... by setting WSGIPythonPath /home/pi/web/piFlask/venv3/lib/python3.4/site-packages in /etc/apache2/mods-available/wsgi.conf

But I found an instruction somewhere saying you have to compile mod_wsgi for Py3, and the bottom quickly falls out to this problem.

解决方案

Correct, mod_wsgi needs to be compiled for a specific Python version as it never actually executes 'python' executable. Instead the Python library is linked into mod_wsgi.

The end result is you cannot mix Python 3 code within an application running using the Python 2 interpreter.

You would have to consider making your code runnable under both Python 2 and 3 and then you could choose which of those you want to use and use a version of mod_wsgi compiled for the version you choose to use.

这篇关于让Flask使用Python3(Apache / mod_wsgi)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆