当由
python manage.py runserver
在 python 3.9 虚拟环境中启动时,我的 django 服务器工作正常,但是当由 apache 启动并将相同的虚拟环境配置到 python 路径时,它会失败并出现以下错误:
[Mon Mar 11 11:00:49.683439 2024] [wsgi:error] [pid 909852:tid 139790141196032] [remote 10.144.196.5:52892] from .ldapauth import *
[Mon Mar 11 11:00:49.683446 2024] [wsgi:error] [pid 909852:tid 139790141196032] [remote 10.144.196.5:52892] File "/srv/django/pxpro/pxpro_editor/ldapauth.py", line 4, in <module>
[Mon Mar 11 11:00:49.683495 2024] [wsgi:error] [pid 909852:tid 139790141196032] [remote 10.144.196.5:52892] import ldap
[Mon Mar 11 11:00:49.683501 2024] [wsgi:error] [pid 909852:tid 139790141196032] [remote 10.144.196.5:52892] File "/srv/django/pxpro-env/lib/python3.9/site-packages/ldap/__init__.py", line 34, in <module>
[Mon Mar 11 11:00:49.683504 2024] [wsgi:error] [pid 909852:tid 139790141196032] [remote 10.144.196.5:52892] import _ldap
[Mon Mar 11 11:00:49.683512 2024] [wsgi:error] [pid 909852:tid 139790141196032] [remote 10.144.196.5:52892] ModuleNotFoundError: No module named '_ldap'
我在 python 虚拟环境中使用 django 4.2.10 和 pyldap==3.0.0.post1 和 python-ldap 3.4.4
我的apache.conf:
$ cat /etc/httpd/conf.d/pxpro.conf
<VirtualHost *:8031>
ServerAdmin <myserverAdmin>
ServerName <myServer>
# ServerAlias <server_fqdn>
ErrorLog /var/log/httpd/pxpro-error_log
CustomLog /var/log/httpd/pxpro-access_log combined
LogLevel debug
HostnameLookups Off
UseCanonicalName Off
ServerSignature Off
Alias /media/ /srv/django/pxpro/media/
Alias /static/ /srv/django/pxpro/static/
#RedirectMatch 301 "^/static/media/uploads/(.*)$" "/media/uploads/$1"
<IfModule wsgi_module>
WSGIDaemonProcess pxpro_wsgi user=django group=django home=/srv/django python-home=/srv/django/pxpro-env startup-timeout=15 python-path=/srv/django/pxpro-env/lib/python3.9/site-packages
WSGIProcessGroup pxpro_wsgi
WSGIApplicationGroup pxpro_wsgi
WSGIScriptAlias / /srv/django/pxpro/settings/wsgi.py process-group=pxpro_wsgi application-group=pxpro_wsgi
</IfModule>
...
知道这怎么可能吗?
来自pyldap的PYPI
页面不幸的是,由于 pip bug 4961,使用 pip 从以前的版本升级会使 ldap 模块无法导入。
不要升级,请通过两个单独的步骤将
pyldap
替换为 python-ldap
:
python -m pip uninstall pyldap
python -m pip install python-ldap
如果升级已经出现问题,您可以通过卸载并重新安装来修复您的环境
python-ldap
:
python -m pip uninstall python-ldap
python -m pip install python-ldap
对于给您带来的不便,我们深表歉意。如果您有更好的解决方案,请加入pyldap bug 148的讨论。
所以基本上你必须卸载
pyldap
。