我从 git 克隆 repo。我创建 venv:
python -m venv myenv
/myenv/scripts/activate.bat
pip install -r requirements.txt
pip install mod_wsgi-4.6.5+ap24vc14-cp36-cp36m-win_amd64.whl
如果我从 myvenv 运行:
python manage.py runserver
是工作!
如果我从 apache 运行,则会出现错误:
[Wed Oct 30 10:51:18.732028 2019] [mpm_winnt:notice] [pid 352:tid 168] AH00455: Apache/2.4.41 (Win64) mod_wsgi/4.6.5 Python/3.6 configured -- resuming normal operations
[Wed Oct 30 10:51:18.732028 2019] [mpm_winnt:notice] [pid 352:tid 168] AH00456: Apache Lounge VS16 Server built: Aug 9 2019 16:46:32
[Wed Oct 30 10:51:18.732028 2019] [core:notice] [pid 352:tid 168] AH00094: Command line: 'httpd -d C:/Apache24'
[Wed Oct 30 10:51:18.732028 2019] [mpm_winnt:notice] [pid 352:tid 168] AH00418: Parent: Created child process 1748
Fatal Python error: Py_Initialize: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00000354 (most recent call first):
[Wed Oct 30 10:51:23.677228 2019] [mpm_winnt:crit] [pid 352:tid 168] AH00419: master_main: create child process failed. Exiting.
httpd.conf 下面:
LoadFile "c:/<>/python/python36/python36.dll"
LoadModule wsgi_module "c:/envs/myproject/lib/site-packages/mod_wsgi/server/mod_wsgi.cp36-win_amd64.pyd"
WSGIScriptAlias / "c:/<myproject>/wsgi.py"
WSGIPythonHome "c:/envs/myproject"
WSGIPythonPath "c:/<myproject>"
Alias /static/ "c:/<myproject>/static/"
<Directory "c:/<myproject>/static">
Require all granted
</Directory>
<Directory c:/<myproject>>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory c:/<myproject>/attachments>
Require all granted
</Directory>
我将 PYTHONHOME 和 PYTHONPATH 设置为“C:\Users\user\AppData\Local\Programs\Python\Python36;C:\Users\user\AppData\Local\Programs\Python\Python36\Scripts”
我看了很多问题,例如:Windows 10 上的致命 Python 错误 ModuleNotFoundError:没有名为“encodings”的模块但这个错误只出现在apache中。
问题是 Windows 上的 Apache 当作为Service 不接听PYTHONHOME
环境变量使用正确的 Python 安装需要它。
如果没有找到它,它会给出一个非常误导性的错误encdongs module not found
Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
要使其正常工作,请在 Windows 终端或 CMD 中将 Apache 作为标准可执行文件运行 -不作为服务:
set PYTHONHOME=<root-of-python-or-conda-env>
httpd // In this case, Apache server picks up PYTHONHOME and comes up
httpd -k start // as a service (need to be admin) - here it DOES NOT pick up PYTHONHOME and fails with the error above
另请注意,根据 mod_wsgi 作者的说法,在 Windows 上,Apache 似乎忽略了WSGIPython主页指令,所以不用费心去那个兔子洞。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)