Recently working with Flask and using Apache2 as WSGI a new error appeared in the apache logs for a new feature added to the API in Flask:

[Tue Dec 03 15:47:14.706011 2019] [wsgi:error] [pid 5892:tid 140174218479360] [client 123.456.789.012:52356] mod_wsgi (pid=5892): Exception occurred processing WSGI script '/srv/myproject/myproject.wsgi'.
[Tue Dec 03 15:47:14.706688 2019] [wsgi:error] [pid 5892:tid 140174218479360] [client 123.456.789.012:52356] Traceback (most recent call last):
[Tue Dec 03 15:47:14.706767 2019] [wsgi:error] [pid 5892:tid 140174218479360] [client 123.456.789.012:52356] File "/srv/myproject/myproject.wsgi", line 3, in <module>
[Tue Dec 03 15:47:14.706784 2019] [wsgi:error] [pid 5892:tid 140174218479360] [client 123.456.789.012:52356] from esMainLogins import app as application
[Tue Dec 03 15:47:14.706877 2019] [wsgi:error] [pid 5892:tid 140174218479360] [client 123.456.789.012:52356] File "/srv/myproject/esMainLogins.py", line 4, in <module>
[Tue Dec 03 15:47:14.706908 2019] [wsgi:error] [pid 5892:tid 140174218479360] [client 123.456.789.012:52356] import myprojectNotebooks
[Tue Dec 03 15:47:14.706930 2019] [wsgi:error] [pid 5892:tid 140174218479360] [client 123.456.789.012:52356] File "/srv/myproject/myprojectNotebooks.py", line 3, in <module>
[Tue Dec 03 15:47:14.706942 2019] [wsgi:error] [pid 5892:tid 140174218479360] [client 123.456.789.012:52356] import json, ldap
[Tue Dec 03 15:47:14.706981 2019] [wsgi:error] [pid 5892:tid 140174218479360] [client 123.456.789.012:52356] ImportError: No module named 'ldap'

The module "ldap" was not being found by python running inside apache, that's a fact.

The problem appears because I’ve installed the wrong WSGI module  for apache:

root@espuma:/srv/myproject# dpkg -l |grep libapache
ii libapache2-mod-wsgi-py3 4.5.11-1 amd64 Python 3 WSGI adapter module for Apache

I hadn’t installed the ldap module for Python 3.5 (but I did for python 2.7) (this is one of the reasons to use virtual environments more often, my fault).

Anyway, all my development is currently in Python 2.7, so I won’t need that module but the 2.7 version:

root@espuma:/srv/# dpkg -r libapache2-mod-wsgi-py3
root@espuma:/srv/myproject# apt-get install libapache2-mod-wsgi
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
libpython3.5
Use 'apt autoremove' to remove it.
The following NEW packages will be installed:
libapache2-mod-wsgi
0 upgraded, 1 newly installed, 0 to remove and 66 not upgraded.
Need to get 0 B/91.1 kB of archives.
After this operation, 272 kB of additional disk space will be used.
Selecting previously unselected package libapache2-mod-wsgi.
(Reading database ... 127591 files and directories currently installed.)
Preparing to unpack .../libapache2-mod-wsgi_4.5.11-1_amd64.deb ...
Unpacking libapache2-mod-wsgi (4.5.11-1) ...
Setting up libapache2-mod-wsgi (4.5.11-1) ...
apache2_invoke: Enable module wsgi
root@espuma:/srv/myproject# /etc/init.d/apache2 restart
[ ok ] Restarting apache2 (via systemctl): apache2.service.

I’ve printed the sys environment of python following the advice in this serverfault post and it confirmed what I already knew.