mod_wsgi: unable to load the file system codec - Windows 2012R2, Apache2.4 and Python 3.7

I an getting this error: [wsgi:info] […] mod_wsgi (pid=4444): Python home E:\venvs\meteoapi. [wsgi:info] […] mod_wsgi (pid=4444): Initializing Python. Fatal Python error: initfsencoding: unable to load the file system codec ModuleNotFoundError: No module named ‘encodings’

What I did is the following, while in Windows Server 2012R2:

Install Python 3.7 32bit with symbols Install MS C++ compiler VC14 Install Apache 2.4.41 VC14 32bit in default c:\Apache24 Create virtualenv in E:\venvs\meteoapi Activate virtualenv running E:\venvs\meteoapi\Scripts\activate.bat install mod_wsgi-express with: pip install mod_wsgis This created ‘mod_wsgi.cp37-win32.pyd’ in e:/venvs/meteoapi/lib/ Run from activated venv mod_wsgi-express module-config Copy output to httpd.conf to load mod_wsgi in Apache. LoadModule wsgi_module "e:/venvs/meteoapi/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win32.pyd" WSGIPythonHome "e:/venvs/meteoapi" Start Apache2.4 with: c:\Apache24\bin\httpd.exe

Apache doesn’t start but error.logs shows what reported above:

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

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 78 (30 by maintainers)

Most upvoted comments

I just tried the zip with “–no-cache-dir”. It seems to be working alright. Clean Apache error.log

I could finally made at start the Django application in Windows and with Virtualenv activated.

The final Django wsgy.py

import os, sys
import site

abs_file = os.path.abspath(__file__)
base_app = os.path.dirname(os.path.dirname(abs_file))

# Virtualenv 20.x automatically adds to System Environmental variables a var named VIRTUAL_ENV
# Such var points to the curren active virtual environment, so we can get it to force loading osite-packages
VIRTUAL_ENV = os.getenv("VIRTUAL_ENV")

# add the django project path itself into the sys.path (not the 'django' module)
site.addsitedir(base_app)
# add site-packages from virtualenv, including 'django' and any other requirement
# note that just packages with their own folder set in site-packages are detected. Not simple .py scripts in /site-packages root dir.
site.addsitedir(VIRTUAL_ENV + '/Lib/site-packages')

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'meteoapi.settings')

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

This was paired with loading the mod_wsgi module in httpd.conf:

LoadModule wsgi_module "e:/venvs/meteo64/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"

andthe proper VirtualHost in httpd-vhosts.conf

<VirtualHost *:80>
    ServerAdmin my@mail.com
    ServerName myservername
    ServerAlias myservername
    DocumentRoot "E:/wsgi/meteoapi/"      
    Alias /static "E:/wsgi/meteoapi/static"
    <Directory E:/wsgi/meteoapi/static>
        Require all granted
    </Directory>    
     WSGIScriptAlias / "E:/wsgi/meteoapi/meteoapi/wsgi.py"
    <Directory E:/wsgi/meteoapi/meteoapi/>
        AllowOverride none
        Require all granted
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
</VirtualHost>

Dear @shippo16 on Windows there is a know HACK to make it work. It’s main downside is that using it will allow you to run JUST ONE wsgi Django app on one Apache2 instance as the hack is using global configuration: any wsgi app will use THE SAME virtualenv.

The hack consists in adding ONLY the following lines in httpd.conf (no other WSGI-related line must be present)

LoadModule wsgi_module “%absolute-path-to-venv-folder%/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd” WSGIVerboseDebugging On WSGIApplicationGroup %{GLOBAL} WSGIPythonPath “%absolute-path-to-venv-folder%/Lib/site-packages;%absolute-path-to-django-project-root-folder%”

To be clear, %absolute-path-to-django-project-root-folder% is the path to the folder containing “manage.py”. %absolute-path-to-venv-folder% is the path to the Virtualenv folder where all dependencies for your project have been installed

DO NOT ADD directive WSGIPythonHome -> will lead to “ModuleNotFoundError: No module named ‘encodings’”