简体   繁体   English

Python Flask 在 Apache 中使用 mod_wsgi; 如何让 venv 工作?

[英]Python Flask using mod_wsgi in Apache; how to get venv to work?

I've been trying to get this to work, and have searched everywhere and read page up and page down, but doesn't seem to find an answer.我一直在努力让它发挥作用,到处搜索,上下翻页,但似乎没有找到答案。

I have Apache with mod_wsgi and a simple test Flask application.我有带有 mod_wsgi 的 Apache 和一个简单的测试 Flask 应用程序。

I got it working using this: https://www.jakowicz.com/flask-apache-wsgi/我用这个让它工作: https://www.jakowicz.com/flask-apache-wsgi/

Then I somehow found that Apache mod_wsgi used the system python and I want to use venv ( https://docs.python.org/3/library/venv.html ).然后我以某种方式发现 Apache mod_wsgi 使用了系统 python 并且我想使用 venv ( https://docs.python.org/3/library/venv.html )。

My application is in a directory with the normal directory structure of the venv, but how do I get my application to use that?我的应用程序位于具有 venv 正常目录结构的目录中,但如何让我的应用程序使用它?

I found this: http://blog.dscpl.com.au/2014/09/using-python-virtual-environments-with.html我发现了这个: http://blog.dscpl.com.au/2014/09/using-python-virtual-environments-with.html

But if I put in python-home my application fails.但是,如果我放入 python-home,我的应用程序就会失败。

A couple of questions: How do I find the Python version that my app is using?几个问题:如何找到我的应用程序使用的 Python 版本? How do I find my mod_wsgi version?我如何找到我的 mod_wsgi 版本? How do I get my app to use my venv?如何让我的应用程序使用我的 venv?

I'm new to Python and WSGI, I have mostly worked with PHP.我是 Python 和 WSGI 的新手,我主要使用 PHP。

Hope someone can explain to me what to do...希望有人可以向我解释该怎么做...

-- Ronni -- 罗尼

How do I find the Python version that my app is using? 如何找到我的应用程序正在使用的Python版本?

See the documentation: 请参阅文档:

it provides you a test you can use. 它为您提供了可以使用的测试。

How do I find my mod_wsgi version? 我如何找到我的mod_wsgi版本?

Use the mod_wsgi.version key from the WSGI environ dictionary. 使用WSGI environ词典中的mod_wsgi.version键。 See reference to this in: 请参阅以下内容中的参考:

You can also use: 您还可以使用:

import mod_wsgi
print(mod_wsgi.version)

How do I get my app to use my venv? 如何获得我的应用程序以使用我的venv?

Documentation on using virtual environments can be found at: 可以在以下位置找到有关使用虚拟环境的文档:

As it states, if your mod_wsgi uses system Python and you want to use a different Python installation or version, you cannot force it to use that other installation or version. 如状态所示,如果您的mod_wsgi使用系统Python,并且您想使用其他Python安装或版本,则不能强制其使用其他安装或版本。 The mod_wsgi binary must be compiled against the specific Python version you want to use. 必须针对要使用的特定Python版本编译mod_wsgi二进制文件。

Because system mod_wsgi packages are usually ancient and not for the version of Python you want to use, you should uninstall the system mod_wsgi package and install mod_wsgi from source code against the Python version you want to use. 由于系统mod_wsgi软件包通常是较旧的版本,而不是您要使用的Python版本,因此您应卸载系统mod_wsgi软件包,并针对要使用的Python版本从源代码安装mod_wsgi。 For that the easiest install method is using pip install mod_wsgi . 为此,最简单的安装方法是使用pip install mod_wsgi See: 看到:

Continuing the discussion in the comment section of your question: 继续问题的评论部分中的讨论:

Assume the following directory with a virtual environment created in the folder /venv : 假设以下目录具有在/venv文件夹中创建的虚拟环境:

- main.py
- /static
  - /js
  - /html
  - /css
- /venv
  - /bin
    - activate

To activate the virtual environment, thus using the local copy of Python (as opposed to your global copy), the following command must be used: 要激活虚拟环境,从而使用Python的本地副本(而不是全局副本),必须使用以下命令:

. /venv/bin/activate

the . . essentially tells the terminal window that the activate file, located at /venv/bin (assuming we're in the top level of the above directory), must be executed. 本质上是告诉终端窗口必须执行位于/venv/bin (假设我们在上述目录的顶层)中的activate文件。 You'll know that the command is successful when you see the string (venv) at the start of a new line in your terminal window. 当您在终端窗口中的新行的开头看到字符串(venv)时,您将知道命令成功。

Now, the which command will confirm that you're now using the local copy of Python: 现在, which命令将确认您正在使用Python的本地副本:

which python

Now your virtual environment is activated, you can use pip to install any module you wish locally to this virtual environment. 现在您的虚拟环境已激活,您可以使用pip在该虚拟环境中本地安装所需的任何模块。 You can specify to install a certain version of a module if you wish, or just grab the latest. 您可以根据需要指定安装模块的特定版本,也可以仅安装最新版本。 The version of Flask or Apache that you install depends on what you specify when installing. 瓶或Apache的,你安装的版本取决于指定安装时什么。

Lastly, the command python --version will tell you the version of this copy of Python 2. python3 --version will do the same for Python 3. Whenever you execute a script using this local copy of Python, it will be using this Python version. 最后,命令python --version将告诉您此Python 2副本的版本python3 --version将对Python 3执行相同的操作。每当您使用此Python本地副本执行脚本时,它将使用此Python。版。

To get Python version from within a script: 要从脚本中获取Python版本:

from sys import version_info   
print version_info

Output (will depend on your version, but will look something like this): 输出(取决于您的版本,但是看起来像这样):

sys.version_info(major=2, minor=7, micro=13, releaselevel='final', serial=0)

The best and most elegant way, I find, is simply specifying the python interpreter that lies within the environment itself.我发现最好和最优雅的方法是简单地指定位于环境本身内的 python 解释器。 Just start your wsgi file with:只需使用以下命令启动您的 wsgi 文件:

#!/path/to/your/venv/bin/python

Et voilà.等等。

I wrote ablog pos t on this topic, hope it would help.我写了一篇关于这个主题的博客文章,希望它能有所帮助。 The most important thing is that the python version associated with the mod_wsgi needs to match the python version used in the pip virtual environment.最重要的是,与mod_wsgi关联的python版本需要匹配pip虚拟环境中使用的python版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM