Weasyprint: OSError: cannot load library 'gobject-2.0-0' while trying to deploy Django project
Hi everyone, I'm a student in software development and new to Django and Railway. Me and my friends we have build a little app in Django and that app uses weasyprint to generate a pdf report file. We decided to deploy on Railway.
Unfortunately, the deployement crashes and we'are getting that error log:
from . import views File "/app/DepistClic/mainapp/views.py", line 6, in <module> from weasyprint import HTML, CSS ...
File "/opt/venv/lib/python3.10/site-packages/cffi/api.py", line 832, in _make_ffi_library backendlib = _load_backend_lib(backend, libname, flags) File "/opt/venv/lib/python3.10/site-packages/cffi/api.py", line 827, in _load_backend_lib raise OSError(msg) OSError: cannot load library 'gobject-2.0-0': gobject-2.0-0: cannot open shared object file: No such file or directory. Additionally, ctypes.util.find_library() did not manage to locate a library called 'gobject-2.0-0'I have spent hours searching a solution. I understand that some additional packages are needed as said in that documentation https://doc.courtbouillon.org/weasyprint/stable/first_steps.html#ubuntu-20-04 So I tried to add them in a nixpacks.toml file at the root of my project:
[phases.setup]
aptPkgs = ["...", "python3-pip", "python3-cffi", "python3-brotli", "libpango-1.0-0", "libharfbuzz0b", "libpangoft2-1.0-0"]
And nixpacks seems to setup these packages (see the image). But I still got hte error "OSError: cannot load library 'gobject-2.0-0': gobject-2.0-0: cannot open shared object file: No such file or directory. Additionally, ctypes.util.find_library() did not manage to locate a library called 'gobject-2.0-0'"And deployement inevitably crashes. I would really appreciate if anyone could help us to resolve this issue. I feel that I'm close to the solution but I miss something. Thanks a lot !
Solution:Jump to solution
Thanks a lot for your response. I really appreciate that ! While waiting, I've read posts from people with the same problem and after multiples attempts I managed to resolve the issue with this dockerfile:
`FROM python:3.10.12
RUN apt-get update
RUN apt-get -y install python3-pip python3-cffi python3-brotli libpango-1.0-0 libpangoft2-1.0-0...
11 Replies
Project ID:
d616bd58-0440-4197-b741-24b36e54d293
Project ID: d616bd58-0440-4197-b741-24b36e54d293
Hey man,
Here's setup with weasyprint and flask, should work just fine on django
Dockerfile
requirements.txt
main.py
project structure
This setup works out of the box deploying on railways, just add domain
boto3 can be changed to anything you prefer to use as a file serving to user
Solution
Thanks a lot for your response. I really appreciate that ! While waiting, I've read posts from people with the same problem and after multiples attempts I managed to resolve the issue with this dockerfile:
FROM python:3.10.12
RUN apt-get update
RUN apt-get -y install python3-pip python3-cffi python3-brotli libpango-1.0-0 libpangoft2-1.0-0
WORKDIR /app
RUN pip install pipenv
COPY Pipfile Pipfile.lock ./
RUN pipenv install --deploy --ignore-pipfile
COPY . ./
CMD pipenv run python DepistClic/manage.py migrate && pipenv run python DepistClic/manage.py collectstatic --no-input && pipenv run gunicorn locallibrary.wsgi
Thanks a lot for your help again
Awesome!
Nice find, yeah docker is a lot easier to manage and more flexible
I'm still on the deployement, I hope that this issue will not surface again. Just one question, I do not master Docker so I'm learning on the road. Is it mandatory to add "EXPOSE 8081" or "EXPOSE 8000" to the dockerfile ?
Everyone who resolved the weasyprint issue seem to have used the dockerfile with these settings
Not mandatory, but a good practice for future development
If you still have issues with Dockerfile you sent try one I sent, tested it 5 minutes ago works great 😅
Just one key thing
Let application discover port that has been set by railway you can also override that setting
https://docs.railway.app/deploy/exposing-your-app
Thanks a lot for your help ! I will do that