GrahamDumpleton / mod_wsgi

Source code for Apache/mod_wsgi.
Apache License 2.0
1.03k stars 269 forks source link

ImportError: cannot import name 'encodings' from 'psycopg2._psycopg' (again) #915

Open parmentelat opened 1 week ago

parmentelat commented 1 week ago

this issue resonates with #753 however I have not been able to solve my problem from that issue, so here is a new one

in my case I have a plain fedora41 box freshly installed, trying to run an application that was working fine within fedora40, but that fails in this new environment

I have python3 and python3-psycopg2 installed through dnf from fedora and as far as the terminal, this works fine, in that I can do

python -c "import psycopg2"

however when invoked through wsgi I am getting the error quoted in the title, and more specifically

Traceback (most recent call last):
  File "/usr/share/plc_api/apache/plc.wsgi", line 35, in application
    api = PLCAPI()
  File "/usr/share/plc_api/PLC/API.py", line 160, in __init__
    from PLC.PostgreSQL import PostgreSQL
  File "/usr/share/plc_api/PLC/PostgreSQL.py", line 15, in <module>
    import psycopg2
  File "/usr/lib64/python3.13/site-packages/psycopg2/__init__.py", line 67, in <module>
    from psycopg2 import extensions as _ext
  File "/usr/lib64/python3.13/site-packages/psycopg2/extensions.py", line 50, in <module>
    from psycopg2._psycopg import (                             # noqa
    ...<5 lines>...
        set_wait_callback, get_wait_callback, encrypt_password, )
ImportError: cannot import name 'encodings' from 'psycopg2._psycopg' (/usr/lib64/python3.13/site-packages/psycopg2/_psycopg.cpython-313-x86_64-linux-gnu.so)
11-20 17:39:41 ERROR plc.wsgi:56 INTERNAL ERROR !!

the apache config reads

<VirtualHost *:443>

   # SSL
   SSLEngine On
   SSLCertificateFile /etc/planetlab/www_ssl.crt
   SSLCertificateKeyFile /etc/planetlab/www_ssl.key
   SSLCertificateChainFile /etc/planetlab/www_ca_ssl.crt

   WSGIScriptAlias /PLCAPI /usr/share/plc_api/apache/plc.wsgi
   WSGIDaemonProcess plcapi-wsgi-ssl user=apache group=apache processes=1 threads=25
   WSGIProcessGroup plcapi-wsgi-ssl

   <Directory "/usr/share/plc_api/apache">
      Options +ExecCGI
      Require all granted
   </Directory>

</VirtualHost>

the puzzling thing is that again this was working fine within fedora40 - which was using python-3.12 instead of 3.13 here, but well that does not seem too relevant..

there is no virtualenv in this box, just one plain system python install

let me know if I can make anything clearer

GrahamDumpleton commented 1 week ago

Try changing to use:

   WSGIScriptAlias /PLCAPI /usr/share/plc_api/apache/plc.wsgi
   WSGIDaemonProcess plcapi-wsgi-ssl user=apache group=apache processes=1 threads=25
   WSGIProcessGroup plcapi-wsgi-ssl
   WSGIApplicationGroup %{GLOBAL}

In other words, add within the VirtualHost definition:

   WSGIApplicationGroup %{GLOBAL}

I don't know why but some versions of psycopg2 in the past haven't worked properly when used in a Python sub interpreter.

That extra directive will force the Python WSGI application to run in the main interpreter context of the mod_wsgi daemon process instead of a sub interpreter context.

parmentelat commented 1 week ago

That did the trick indeed ! thanks a million :)

for the record the app was working just fine in fedora 39, and I ran into this when upgrading to fedora41

here are a few possibly relevant changes: