如何让 mod_wsgi 拿起我的 virtualenv

How to get mod_wsgi to pick up my virtualenv

我对使用 Flask 很陌生-

根据http://flask.pocoo.org/docs/0.12/deploying/mod_wsgi/

标题下 "Working with Virtual Environments" 我读到:

For Python 3 add the following lines to the top of your .wsgi file:

activate_this = '/path/to/env/bin/activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this)) This sets up the load paths according to the settings of the virtual environment.

Keep in mind that the path has to be absolute.

激活我的 venv 我使用来自 linux 的命令:

 my_env/bin/activate

我查看了我的 my_env/bin/ 目录,但没有看到任何 .py 文件。 我是否应该在 my_env/bin/ 中创建一个将由 .wsgi 文件调用的 .py 文件?

如果您使用的是 mod_wsgi,请阅读文档:

TLDR:

From Documentation - to use a Python virtual environment, all you need to do is add the python-home option to the WSGIDaemonProcess directive resulting in

将此行添加到您的虚拟主机以启用 virtualenv

WSGIDaemonProcess application_name python-home=/path/to/app/venv

我遇到了同样的问题,解决方法其实很简单。您需要安装 libapache2-mod-wsgi-py3 而不是 libapache2-mod-wsgi。后者用于 python 2.

然后您可以通过将环境的站点包添加到系统路径来激活您的环境。例如,对我来说(使用 venv),我可以通过将以下行添加到我的 *.wgsi 文件中来做到这一点。

sys.path.insert(0,"/path/to/venv/lib/python3.8/site-packages")

我发现最好和最干净的方法是在不对晦涩的脚本进行某种“魔术”的情况下,简单地以对位于环境中的 python 解释器的引用开始 .wsgi。用这个开始你的 .wsgi,之后不需要 fiddle:

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

我希望我在花费数小时未成功之前考虑过这个简单的解决方案 - 并希望其他人已经提到它。