我在 Windows 10 上运行 Python 3.7.1 和 Apache 2.4.38。我设置了一个虚拟环境,其中包含通过“pip”安装的 Django 2.2.5 和 mod_wsgi 4.6.5。在“httpd.conf”内部,我按照几条指令将 WSGIPythonHome 设置为指向虚拟环境的根目录。但是当我启动 Apache 时,它失败并显示:“没有名为‘编码’的模块”。 Apache 似乎需要访问系统范围内的 Python。
我在“C:\Program Files\Python3.7.1”中找到了系统范围的Python。
虚拟环境位于:“D:\PROJ\PYTHON\VEnv\django”。
这是我添加到“httpd.conf”中的内容:
LoadModule wsgi_module "D:/PROJ/PYTHON/VEnv/django/Lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIScriptAlias / "D:/PROJ/PYTHON/VEnv/django/Shipkaliev/wsgi.py"
WSGIPythonHome "D:/PROJ/PYTHON/VEnv/django"
WSGIPythonPath "D:/PROJ/PYTHON/VEnv/django/Shipkaliev"
<Directory "D:/PROJ/PYTHON/VEnv/django/Shipkaliev">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
根据:https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html and: https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/modwsgi/ https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/modwsgi/这就是需要设置的方式。
因此,使用此配置,Apache 无法找到虚拟环境中不存在的所有本机 Python 模块,如“编码”、“编解码器”、“io”等。
当我注释掉“WSGIPythonHome”指令时,Apache 初始化了 Python,但是如果我尝试在“wsgi.py”脚本中导入 Django,它无法找到它。
当我将“WSGIPythonHome”更改为指向系统范围的Python并将虚拟环境的“站点包”添加到路径时,我让一切正常运行的方法是:
WSGIPythonHome "C:/Program Files/Python3.7.1"
WSGIPythonPath "D:/PROJ/PYTHON/VEnv/django/Lib/site-packages;D:/PROJ/PYTHON/VEnv/django/Shipkaliev"
但这并不是它的预期工作方式。 “WSGIPythonHome”有什么意义?难道我做错了什么?我的解决方案可以接受吗?
非常感谢!