Django-server fails for ModuleNotFoundError: No module named '_ldap' when started by apache
When started in python 3.9 virtual environment by python manage.py runserver my django-server works fine, but when started by apache with the same virtual environment configured into python path it fails with the errors below:
[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'
I'm using django 4.2.10 with pyldap==3.0.0.post1 and python-ldap 3.4.4 in my python virtual environment
my 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>
...
Any idea how this is possible ?
From PYPI page of pyldap
Warning
Unfortunately, due to pip bug 4961, upgrading from previous versions using pip makes the ldap module unimportable.
Instead of upgrading, please replace pyldap by python-ldap in two separate steps:
python -m pip uninstall pyldap
python -m pip install python-ldap
If upgraded already issue, you can fix your environment by uninstalling and reinstalling python-ldap:
python -m pip uninstall python-ldap
python -m pip install python-ldap
We are sorry for the inconvenience. If you have a better solution, please join the discussion at pyldap bug 148.
So basically you have to uninstall pyldap.