简体   繁体   中英

Django Apache error: No module named 'encodings'. Windows server 2008 R2 Standard

I clone repo from git. I create 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

if i run from myvenv that:

python manage.py runserver

it's work!

if I run from apache, I have a error:

[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.

below 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>

I set PYTHONHOME and PYTHONPATH as "C:\Users\user\AppData\Local\Programs\Python\Python36;C:\Users\user\AppData\Local\Programs\Python\Python36\Scripts"

I looked many question, example: Fatal Python error on Windows 10 ModuleNotFoundError: No module named 'encodings' but this error only in apache.

Your config file seems to have no issue. I had a similar issue, how I solved it was,

I checked my Apache24 installation, it was 32-bit, while my python install was a 64-bit, so I had to reinstall the Apache24 64 bit version and put the configuration in httpd.conf again.

I also had to install my python in the main C directory instead of instead of inside the \Users so my base python location was C:\Python36 , and include the C:\Python36\ and C:\Python36\Scripts\ inside the path of the system variables then create a new virtualenv and include in the Apache configuration

Fortunately, I have done it.

I used "virtualenvwrapper" for Windows. It's really helpful.

pip install virtualenvwrapper-win
mkvirtualenv myenv
pip install -r requirements.txt
pip install mod_wsgi-4.6.5+ap24vc14-cp36-cp36m-win_amd64.whl

It's work.

The issue is that Apache on Windows, when run as a Service does not pick up the PYTHONHOME environment variable which it needs for the right Python installation to be used.

If it doesn't find it, it gives a very misleading error about the encdongs module not found

    Fatal Python error: initfsencoding: unable to load the file system codec
    ModuleNotFoundError: No module named 'encodings'

To get it to work, in a Windows Terminal or CMD, run Apache as a standard executable - not as a Service :

    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

Also note, per the mod_wsgi author, on Windows, Apache seems to ignore the WSGIPythonHome directive, so don't bother going down that rabbit hole.

Ensure you have the PYTHONHOME environment variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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