Search code examples
python-3.xlxmlebooklib

Python - Ebooklib - Lxml: ImportError: Interpreter change detected - this module can only be loaded into one interpreter per process


We have a site where the back-end is in Python 3.x. We have recently added the ebooklib libray, in order to create epub books. Now we regularly get the following error:

    [Fri Jul 28 00:35:40.547413 2023] [wsgi:error] [pid 21108] [client 94.66.158.142:34912] mod_wsgi (pid=21108): Failed to exec Python script file '/var/www/api/api.wsgi'., referer: https://oursite.ourdomain.gr/admin/SignUp
[Fri Jul 28 00:35:40.547477 2023] [wsgi:error] [pid 21108] [client 94.66.158.142:34912] mod_wsgi (pid=21108): Exception occurred processing WSGI script '/var/www/api/api.wsgi'., referer: https://oursite.ourdomain.gr/admin/SignUp
[Fri Jul 28 00:35:40.549324 2023] [wsgi:error] [pid 21108] [client 94.66.158.142:34912] Traceback (most recent call last):, referer: https://oursite.ourdomain.gr/admin/SignUp
[Fri Jul 28 00:35:40.549416 2023] [wsgi:error] [pid 21108] [client 94.66.158.142:34912]   File "/var/www/api/api.wsgi", line 8, in <module>, referer: https://oursite.ourdomain.gr/admin/SignUp
[Fri Jul 28 00:35:40.549426 2023] [wsgi:error] [pid 21108] [client 94.66.158.142:34912]     from api import app as application, referer: https://oursite.ourdomain.gr/admin/SignUp
[Fri Jul 28 00:35:40.549438 2023] [wsgi:error] [pid 21108] [client 94.66.158.142:34912]   File "/var/www/api/api/__init__.py", line 80, in <module>, referer: https://oursite.ourdomain.gr/admin/SignUp
[Fri Jul 28 00:35:40.549445 2023] [wsgi:error] [pid 21108] [client 94.66.158.142:34912]     import api.api.ebooks, referer: https://oursite.ourdomain.gr/admin/SignUp
[Fri Jul 28 00:35:40.549456 2023] [wsgi:error] [pid 21108] [client 94.66.158.142:34912]   File "/var/www/api/api/api/ebooks.py", line 8, in <module>, referer: https://oursite.ourdomain.gr/admin/SignUp
[Fri Jul 28 00:35:40.549464 2023] [wsgi:error] [pid 21108] [client 94.66.158.142:34912]     from helper_funcs.ebooks_creation import create_epub, create_pdf_with_html, referer: https://oursite.ourdomain.gr/admin/SignUp
[Fri Jul 28 00:35:40.549475 2023] [wsgi:error] [pid 21108] [client 94.66.158.142:34912]   File "/var/www/api/api/helper_funcs/ebooks_creation.py", line 6, in <module>, referer: https://oursite.ourdomain.gr/admin/SignUp
[Fri Jul 28 00:35:40.549482 2023] [wsgi:error] [pid 21108] [client 94.66.158.142:34912]     from ebooklib import epub, referer: https://oursite.ourdomain.gr/admin/SignUp
[Fri Jul 28 00:35:40.549491 2023] [wsgi:error] [pid 21108] [client 94.66.158.142:34912]   File "/usr/local/lib/python3.7/dist-packages/ebooklib/epub.py", line 40, in <module>, referer: https://oursite.ourdomain.gr/admin/SignUp
[Fri Jul 28 00:35:40.549498 2023] [wsgi:error] [pid 21108] [client 94.66.158.142:34912]     raise ex, referer: https://oursite.ourdomain.gr/admin/SignUp
[Fri Jul 28 00:35:40.549539 2023] [wsgi:error] [pid 21108] [client 94.66.158.142:34912]   File "/usr/local/lib/python3.7/dist-packages/ebooklib/epub.py", line 36, in <module>, referer: https://oursite.ourdomain.gr/admin/SignUp
[Fri Jul 28 00:35:40.549546 2023] [wsgi:error] [pid 21108] [client 94.66.158.142:34912]     from ebooklib.utils import parse_string, parse_html_string, guess_type, get_pages_for_items, referer: https://oursite.ourdomain.gr/admin/SignUp
[Fri Jul 28 00:35:40.549556 2023] [wsgi:error] [pid 21108] [client 94.66.158.142:34912]   File "/usr/local/lib/python3.7/dist-packages/ebooklib/utils.py", line 20, in <module>, referer: https://oursite.ourdomain.gr/admin/SignUp
[Fri Jul 28 00:35:40.549562 2023] [wsgi:error] [pid 21108] [client 94.66.158.142:34912]     from lxml import etree, referer: https://oursite.ourdomain.gr/admin/SignUp
[Fri Jul 28 00:35:40.549589 2023] [wsgi:error] [pid 21108] [client 94.66.158.142:34912] ImportError: Interpreter change detected - this module can only be loaded into one interpreter per process., referer: https://oursite.ourdomain.gr/admin/SignUp

A google search led us to this. So we added WSGIApplicationGroup %{GLOBAL} to both oursite.ourdomain.gr.conf and oursite.ourdomain.gr-ssl.conf and restarted apache. Didn't work. We also found this, which says to add single-interpreter = true to app.ini of uwsgi. Our system admin says we don't use uwsgi, and he doesn't know where the app.ini mentioned is to be found. We also thought maybe it was a case of a circular imports in ebooklib, as it is rather old. The error seems to happen when epub.py imports ebooklib.utils. Both epub.py and utils.py have

from lxml import etree

at the beginning. So we removed the import from the beginning of the files, and moved it to each function instead. Also didn't work.

Any advice welcome, and also any clarification about the app.ini mentioned above.


Solution

  • So, the system admin added WSGIApplicationGroup %{GLOBAL} to a third file, this time the api.conf file on apache. Haven't had any errors since. So I think that was the solution.