You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Win11下Apache+mod_wsgi启动报错:找不到'encodings'模块

解决Apache+mod_wsgi启动时的ModuleNotFoundError: No module named 'encodings'错误

环境信息

  • Win11系统
  • Apache 2.4
  • mod_wsgi 4.9.4
  • Python 3.10

问题描述

启动Apache后,错误日志持续出现ModuleNotFoundError: No module named 'encodings',即使删除httpd.conf中的VirtualHost配置,错误依然存在。已尝试设置PYTHONHOME和PYTHONPATH环境变量,问题未解决;怀疑虚拟环境未正确激活,但无法定位错误来源、涉及的Python文件及排查方向。

相关配置文件

Apache httpd.conf配置

LoadFile "C:/users/pvachal/appdata/local/programs/python/python310/python310.dll"
LoadModule wsgi_module "C:/users/pvachal/appdata/local/programs/python/python310/lib/site-packages/mod_wsgi/server/mod_wsgi.cp310-win_amd64.pyd"
WSGIPythonHome "C:/users/pvachal/appdata/local/programs/python/python310"

#WSGIDaemonProcess flaskapp python-path="c:/users/pvachal/appdata/local/programs/python/python310/lib/site-packages"

<VirtualHost *:80>
    DocumentRoot "c:/users/pvachal/documents/projekty/exp_app"

    <Directory c:/users/pvachal/documents/projekty/exp_app>
        <IfVersion < 2.4>
            Order allow,deny
            Allow from all
        </IfVersion>
        <IfVersion >= 2.4>
            Require all granted
        </IfVersion>
    </Directory>

    WSGIScriptAlias / "c:/users/pvachal/documents/projekty/exp_app/app.wsgi"

    <Directory c:/users/pvachal/documents/projekty/exp_app>
        <IfVersion < 2.4>
            Order allow,deny
            Allow from all
        </IfVersion>
        <IfVersion >= 2.4>
            Require all granted
        </IfVersion>
    </Directory>

</VirtualHost>

错误日志输出

[Wed Mar 01 15:08:51.465080 2023] [mpm_winnt:notice] [pid 5884:tid 344] AH00428: Parent: child process 21996 exited with status 1 -- Restarting.
[Wed Mar 01 15:08:51.496461 2023] [ssl:warn] [pid 5884:tid 344] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Wed Mar 01 15:08:51.513172 2023] [mpm_winnt:notice] [pid 5884:tid 344] AH00455: Apache/2.4.54 (Win64) OpenSSL/1.1.1p PHP/8.2.0 mod_wsgi/4.9.4 Python/3.10 configured -- resuming normal operations
[Wed Mar 01 15:08:51.513172 2023] [mpm_winnt:notice] [pid 5884:tid 344] AH00456: Apache Lounge VS16 Server built: Jun 22 2022 09:58:15
[Wed Mar 01 15:08:51.513172 2023] [core:notice] [pid 5884:tid 344] AH00094: Command line: 'c:\\xampp\\apache\\bin\\httpd.exe -d C:/xampp/apache'
[Wed Mar 01 15:08:51.514739 2023] [mpm_winnt:notice] [pid 5884:tid 344] AH00418: Parent: Created child process 20496
[Wed Mar 01 15:08:51.700090 2023] [ssl:warn] [pid 20496:tid 336] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
Python path configuration:
  PYTHONHOME = (not set)
  PYTHONPATH = (not set)
  program name = 'python'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = 'C:\\xampp\\apache\\bin\\httpd.exe'
  sys.base_prefix = 'C:\\Users\\pvachal\\AppData\\Local\\Programs\\Python\\Python310'
  sys.base_exec_prefix = 'C:\\Users\\pvachal\\AppData\\Local\\Programs\\Python\\Python310'
  sys.platlibdir = 'lib'
  sys.executable = 'C:\\xampp\\apache\\bin\\httpd.exe'
  sys.prefix = 'C:\\Users\\pvachal\\AppData\\Local\\Programs\\Python\\Python310'
  sys.exec_prefix = 'C:\\Users\\pvachal\\AppData\\Local\\Programs\\Python\\Python310'
  sys.path = [
    'C:\\Users\\pvachal\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip',
    '.\\DLLs',
    '.\\lib',
    'C:\\xampp\\apache\\bin',
  ]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00004a38 (most recent call first):
  <no Python frame>
[Wed Mar 01 15:08:51.778876 2023] [mpm_winnt:notice] [pid 5884:tid 344] AH00430: Parent: Child process 20496 exited successfully.

Flask应用app.wsgi文件

#! C:/Users/pvachal/AppData/Local/Programs/Python/Python310/python.exe

import os, sys

sys.path.insert(0, "c:/users/pvachal/documents/projekty/exp_app")

activate_this = "c:/users/pvachal/documents/projekty/exp_app/venv/scripts/activate_this.py"
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

from app import app as application

问题根源分析

从错误日志看,Python初始化时无法找到encodings模块,核心原因是Python的路径配置异常:

  • 日志显示PYTHONHOME = (not set),尽管httpd.conf中配置了WSGIPythonHome,但可能因为Apache服务启动时未正确读取该配置,或者路径格式问题(比如大小写、斜杠方向)
  • sys.path中的.\\DLLs和.\\lib是相对路径,Apache的工作目录是C:\\xampp\\apache\\bin,所以相对路径指向的是C:\\xampp\\apache\\bin\\DLLs和C:\\xampp\\apache\\bin\\lib,而非Python安装目录下的对应文件夹,导致无法找到核心模块

解决步骤

  1. 修正WSGIPythonHome配置格式
    将httpd.conf中的路径统一使用正斜杠或双反斜杠,确保路径准确且无大小写问题:

    WSGIPythonHome "C:/Users/pvachal/AppData/Local/Programs/Python/Python310"
    
  2. 添加WSGIPythonPath配置
    显式指定Python的Lib和DLLs目录,避免相对路径错误:

    WSGIPythonPath "C:/Users/pvachal/AppData/Local/Programs/Python/Python310;C:/Users/pvachal/AppData/Local/Programs/Python/Python310/DLLs;C:/Users/pvachal/AppData/Local/Programs/Python/Python310/lib"
    
  3. 检查Apache服务的启动权限
    Win11下,Apache服务可能以系统账户启动,无法读取用户目录下的Python文件。尝试:

    • 将Python安装到公共目录(如C:\Python310),而非用户AppData目录
    • 右键Apache服务(服务管理器中),选择“属性”-“登录”,改为使用当前用户账户启动
  4. 改用WSGIDaemonProcess管理虚拟环境
    取消httpd.conf中WSGIDaemonProcess的注释,修改为:

    WSGIDaemonProcess flaskapp python-home="c:/users/pvachal/documents/projekty/exp_app/venv" python-path="c:/users/pvachal/documents/projekty/exp_app"
    WSGIProcessGroup flaskapp
    

    同时删除app.wsgi中的虚拟环境激活代码,让mod_wsgi直接管理虚拟环境,避免手动激活导致的路径冲突

  5. 验证Python路径配置
    在httpd.conf中添加调试配置,查看Python路径是否正确加载:

    WSGIScriptAlias /test "c:/users/pvachal/documents/projekty/exp_app/test.wsgi"
    

    创建test.wsgi文件:

    def application(environ, start_response):
        import sys
        start_response('200 OK', [('Content-Type', 'text/plain')])
        return [str(sys.path).encode()]
    

    访问http://localhost/test,查看输出的sys.path是否包含Python核心目录

内容的提问来源于stack exchange,提问作者Petr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 16:07:14