django OSError: no library called "cairo" was found on windows
Solution 1
WeasyPrint needs the Pango, cairo and GDK-PixBuf libraries. They are part of GTK+ (formerly known as GIMP Toolkit), and must be installed separately.
After installing GTK+ libraries, do :
python -m weasyprint http://weasyprint.org weasyprint.pdf
Solution 2
Installing GTK+ didn't work for me. I solved this problem using UniConverter2.0. My environments is
- Python 3.7
- Windows 10 x64
- Install uniconvertor-2.0rc4-win64_headless.msi,
- Find the "dll" sub-directory under the UniConverter installation path.(In my case,
C:\Program Files\UniConvertor-2.0rc4\dlls
) - Add this "dll" path to the system path.
- Close VSCode and reopen the project.
- Try to run the server again. Enjoy!
Solution 3
Starting from Python 3.8, dll's need to be added separately. Added GTK+, MSYS2, Visual Studio C Compiler and Uniconverter. But, nothing seemed to work. Finally, got it working after putting the script for calling add_dll_directory.
import os
def set_dll_search_path():
# Python 3.8 no longer searches for DLLs in PATH, so we have to add
# everything in PATH manually. Note that unlike PATH add_dll_directory
# has no defined order, so if there are two cairo DLLs in PATH we
# might get a random one.
if os.name != "nt" or not hasattr(os, "add_dll_directory"):
return
for p in os.environ.get("PATH", "").split(os.pathsep):
try:
os.add_dll_directory(p)
except OSError:
pass
set_dll_search_path()
Source: PyCairo Windows Python3.8 Import Issue
Solution 4
See the solution here:
https://www.programmersought.com/article/47674569357/
You will need to add a path if not added after installations:
C:\Program Files\GTK3-Runtime Win64\bin
I have been solving it many times this way.
Solution 5
Ok I figure this out. you can have a 64 bit version of python that doesn't work. What I have found to work and this could change is the installing the 64 bit version of python from python's website, not from the Microsoft store!
- remove any version of python you have installed
- download https://www.python.org/ftp/python/3.8.10/python-3.8.10-amd64.exe
- install
- download and install https://github.com/tschoonj/GTK-for-Windows-Runtime-Environment-Installer/releases
- add bin folder from that (sorry it was somebody else's machine, I use Ubuntu) in your environment path.
- run through the first steps on weasyprint and you should be good to go!
Mhmoud Amsha
Updated on July 09, 2022Comments
-
Mhmoud Amsha almost 2 years
When I run the Django server, I see this problem !!
OSError: no library called "cairo" was found no library called "libcairo-2" was found cannot load library 'libcairo.so': error 0x7e cannot load library 'libcairo.2.dylib': error 0x cannot load library 'libcairo-2.dll': error 0x7e
-
Umair almost 4 yearsI am still getting this error even after installing the GTK
-
Mathador over 3 yearsDo not forget to add the installed GTK+ lib to your system path, and restart your IDE so that the changes take effect.
-
Srijwal R over 3 years@Mathador agree.
-
yishairasowsky almost 3 years@Mathador how do i add them to the path?
-
Mathador almost 3 yearsFollow the instructions here: "gisaxs.com/index.php/View.gtk_installation" and replace "C:\GTK\bin" with the actual path where you installed the GTK package on your drive.
-
Nick Garyu over 2 yearsOMG this is what fixed for me after nothing else. Thanks!
-
Didier L over 2 yearsYep, that did the trick! Seems to be an issue with Python installed from the Microsoft Store. I took Python 3.10.1 and it worked for me. Maybe just don’t put a direct link to a specific Python installer, just point to the official website like you did for the GTK installer. Also note that this way of installing Python does not provide the
python3
command-line alias. Just usepython
instead.