I'm running Python 3.7.1 and Apache 2.4.38 on Windows 10. I've set up a virtual environment containing Django 2.2.5 and mod_wsgi 4.6.5 installed via "pip". Inside "httpd.conf" I've set WSGIPythonHome pointing at the root of my virtual environment as per several instructions. But when I start Apache it fails with: "No module named 'encodings'". Apache seems to need access to the system-wide Python.
I've got the system-wide Python in: "C:\Program Files\Python3.7.1". The virtual environment is located in: "D:\PROJ\PYTHON\VEnv\django".
This is what I've added to the "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>
According to: https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html and: https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/modwsgi/ this is how it needs to be set.
So with this configuration Apache fails to find all native Python modules like "encodings", "codecs", "io" and so on, not present within the virtual environment.
When I comment out the "WSGIPythonHome" directive Apache initializes Python, but then if I try to import Django within the "wsgi.py" script, it fails to find it.
The way I got everything working well is when I changed "WSGIPythonHome" to point at the system-wide Python and added the virtual environment's "site-packages" to the path:
WSGIPythonHome "C:/Program Files/Python3.7.1"
WSGIPythonPath "D:/PROJ/PYTHON/VEnv/django/Lib/site-packages;D:/PROJ/PYTHON/VEnv/django/Shipkaliev"
But that's not how it's intended to work. What's the point of "WSGIPythonHome"? Am I doing something wrong? And is my solution acceptable?
Thank you very much!
I found it! Since "D:/PROJ/PYTHON/VEnv/django" is the root of the virtual environment, the Python standard library cannot be found there. The standard library resides in "C:/Program Files/Python3.7.1/Lib". That's why: "No module named 'encodings'".
"WSGIPythonHome - used to indicate to Python when it is initialized where its library files are installed. ... When this directive is used it should be supplied the prefix for the directories containing the platform independent and system dependent Python library files." - thanks to: https://code.google.com/archive/p/modwsgi/wikis/ConfigurationDirectives.wiki#WSGIPythonHome
And that's my final Apache 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;C:/Program Files/Python3.7.1"
WSGIPythonPath "D:/PROJ/PYTHON/VEnv/django/Lib/site-packages;D:/PROJ/PYTHON/VEnv/django/Shipkaliev"
<Directory "D:/PROJ/PYTHON/VEnv/django/Shipkaliev">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
That's not made clear neither in Django nor in mod_wsgi docs. I hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With