Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSError: cannot load library 'gobject-2.0': error 0x7e. #971

Closed
d-vyd opened this issue Oct 16, 2019 · 36 comments
Closed

OSError: cannot load library 'gobject-2.0': error 0x7e. #971

d-vyd opened this issue Oct 16, 2019 · 36 comments

Comments

@d-vyd
Copy link

d-vyd commented Oct 16, 2019

I followed the instructions for installing on Windows with Python 64 bit. I'm using an Anaconda installation but "conda install weasyprint" did not find a package, so I used "pip". I'm getting the error below when trying to test weasyprint. After seeing the error, I tried to manually install "glib" using "conda". It installed. But, did not fix the issue.

C:\Users\david>python -m weasyprint http://weasyprint.org weasyprint.pdf
Traceback (most recent call last):
File "C:\Users\david\Anaconda3\lib\runpy.py", line 183, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "C:\Users\david\Anaconda3\lib\runpy.py", line 142, in _get_module_details
return get_module_details(pkg_main_name, error)
File "C:\Users\david\Anaconda3\lib\runpy.py", line 109, in get_module_details
import(pkg_name)
File "C:\Users\david\Anaconda3\lib\site-packages\weasyprint_init
.py", line 440, in
from .css import preprocess_stylesheet # noqa isort:skip
File "C:\Users\david\Anaconda3\lib\site-packages\weasyprint\css_init
.py", line 30, in
from . import computed_values, media_queries
File "C:\Users\david\Anaconda3\lib\site-packages\weasyprint\css\computed_values.py", line 17, in
from .. import text
File "C:\Users\david\Anaconda3\lib\site-packages\weasyprint\text.py", line 257, in
'libgobject-2.0.dylib')
File "C:\Users\david\Anaconda3\lib\site-packages\weasyprint\text.py", line 253, in dlopen
return ffi.dlopen(names[0]) # pragma: no cover
File "C:\Users\david\Anaconda3\lib\site-packages\cffi\api.py", line 146, in dlopen
lib, function_cache = _make_ffi_library(self, name, flags)
File "C:\Users\david\Anaconda3\lib\site-packages\cffi\api.py", line 828, in _make_ffi_library
backendlib = _load_backend_lib(backend, libname, flags)
File "C:\Users\david\Anaconda3\lib\site-packages\cffi\api.py", line 823, in _load_backend_lib
raise OSError(msg)
OSError: cannot load library 'gobject-2.0': error 0x7e. Additionally, ctypes.util.find_library() did not manage to locate a library called 'gobject-2.0'

@Tontyna
Copy link
Contributor

Tontyna commented Oct 16, 2019

Did you install the/a GTK3-Runtime?

@d-vyd
Copy link
Author

d-vyd commented Oct 17, 2019 via email

@Tontyna
Copy link
Contributor

Tontyna commented Oct 17, 2019

Is gobject related to GTK?

Yes. So. As always when it comes to "OSError cannot load library" on Windows it's a matter of PATH resp. the os.path that Python presents to WeasyPrint.

The fact that weasyprint.text complains about gobject-2 instead of cairocffi complaining about cairo-2 tells me that there is another (reduced) GTK-collection in your os.path.
I assume that Anaconda tweaked the environment and inserted its own library path in front of your GTK3 Runtime folder.

Please run the following command in the shell where you start WeasyPrint and check the output.

python -c "import os; print(*os.environ['PATH'].split(os.pathsep), sep='\n')"

@tyoung88
Copy link

I am not the original poster, but I am having the exact same issue (I also have anaconda installed).
I ran the command that you asked and see that GTK3-Runtime Win65\bin is the first entry.
As an aside I just installed GTK3... restarted my machine to ensure everything was picked up correctly.

Something else I can check?

@tyoung88
Copy link

I checked the folder that the path was pointing to.... I think I found the issue, however I don't understand why it happened.
In the folder where the gtk3 installer ran, the dll file names seem not to be what weasyprint is looking for.
For example in C:\Program Files\GTK3-Runtime Win64\bin I see libgobject-2.0-0.dll instead of libgobject-2.0.dll.
If I manually change the file name so that it matches what weasyprint is looking for, it will load. However it will take me a while to rename all the files.... and I feel there may be an issue I am missing.

let me know if you have any thoughts. Thanks!

@Tontyna
Copy link
Contributor

Tontyna commented Oct 19, 2019

My (main) dll's are named libgobject-2.0-0.dll, libpango-1.0-0.dll, libpangocairo-1.0-0.dll, too . And WeasyPrint has no problem to load them. So that's not the point.
Having no experience with Anaconda but reading a bit in their docs I fear that Anaconda tries hard to provide its users with a solid environment (including DLLs) and doesn't like people loading things unknown to Anaconda.

Isn't there a way to tell Anaconda's environment what paths it should include?
Isn't there a way to install a recent and complete GTK3 (Cairo, GObject, Pango, PangoCairo) with "conda" or whatever?

@Tontyna
Copy link
Contributor

Tontyna commented Oct 19, 2019

Had a closer look at Anaconda. Anaconda is just Python with some comfort.
Thus, as I stated above: To make it use your GTK, your GTK must be at the beginning of the os.environ['PATH'] or at least before all other incompatible, incomplete, unwanted, alternate folders.

@tyoung88 -- did you run the python command that prints the paths from the same command prompt or shell where your WeasyPrint fails with OSError?

I'm quite sure you didn't. I'm quite sure you run WeasyPrint from an Anaconda Prompt or similar that activates Anaconda's environment -- and pushes the Anaconda search paths (including a folder called <Anaconda-root>\Library\mingw-w64\bin) a in front of everything.

There mus be an activation script, probably a .bat or .cmd. You could modify that script to keep your GTK3 folder at the top or you could apply a more Pythonic way and use the famous sitecustomize.py solution:

"""
    sitecustomize.py 
    ================

    To ensure the correct GTK3 Runtime
    
    - To activate the GTK for Anaconda and all its environments:
      put the file into **sys.base_prefix**,
      i.e. the path where Anaconda's master python.exe is located.
      i.e. your Anaconda install directory

    - To activate the GTK only in a dedicated environment:
      put the file into the **./Lib/site-packages** folder of that
      environemt
"""
import os

# insert the GTK3 Runtime folder at the beginning
GTK_FOLDER = r'C:\Program Files\GTK3-Runtime Win64\bin'
os.environ['PATH'] = GTK_FOLDER + os.pathsep + os.environ.get('PATH', '')

No need to rename anything 😁

Be aware that some IDE's try to be helpful and insert folders they consider useful into the os.environ['PATH'], too.

@tyoung88
Copy link

I just wanted to update after such a complete answer @Tontyna .

I am still unsure why the dlls could not be found properly...
I ran the command you asked for in the same command prompt, which gave the results I shared in my previous post. The good news is that after I renamed the dlls and fixed the way I was using the libraries I got things working. Thank you for the tips and I apologize for not having a better answer on the dlls.

@Tontyna
Copy link
Contributor

Tontyna commented Oct 29, 2019

I am still unsure why the dlls could not be found properly...

I have a hypothesis, implying Anaconda's mingw-w64 folder. Especially the fact that renaming your DLLs fixes your WeasyPrint problem is a strong indicator. To verify my assumption I'd have to install Anaconda and I'm not keen to do that...

@chickendude
Copy link

I had a similar issue and it turned out the GTK2 dlls were getting in the way of the GTK3 ones.

@jannefleischer
Copy link

I ran into the same issue in conda and was only able to resolve it, by installing the gtk-libraries from msys2 .

While using the gtk+ 64bit windows installer I got popups on import of weasyprint that said:

"Der Prozedureinsprungspunkt '__cxa_throw_bad_array_new_length' wurde in der DLL 'C:\Program Files\GTK33-Runtime Win64\bin\libgraphite2.dll' nicht gefunden."

(don't know how to translate THAT - who is localizing techy error messages?)

And some other similiar errors.

The initial error said

"OSError: cannot load library 'gobject-2.0': error 0x7e. Additionally, ctypes.util.find_library() did not manage to locate a library called 'gobject-2.0'"

but that isn't actual the error occuring on windows. If I run
from cffi import FFI; ffi=FFI(); ffi.dlopen('libgobject-2.0-0') I get the correct error:

OSError: cannot load library 'C:\Program Files\GTK3-Runtime Win64\bin\libgobject-2.0-0.dll': error 0x7f

And that points to a similiar (and old) issue with cffi:
api.py throws OSError 0x7e when loading libcairo-2.dll

So I am guessing something in conda isn't compatible with the way how the gtk-libraries in the 64bit windows installer are compiled. But on the other hand I may be entirely wrong. I have no clue about compiling anything anywhere.

Long story short: Using msys2 on conda resolved the issue for me.

@Tontyna
Copy link
Contributor

Tontyna commented Jan 4, 2020

So I am guessing something in conda isn't compatible with the way how the gtk-libraries in the 64bit windows installer are compiled

Sounds reasonable.
But: The DLLs in Tom Schoonjans' GTK3 Runtime are copies/extracts from his own up-to-date msys installation, he doesn't compile them...

@kpierce8
Copy link

kpierce8 commented Feb 3, 2020

the sitecustomize.py route worked for me. Just added it to my script before importing weasyprint

@docbrown
Copy link

docbrown commented Mar 8, 2020

I'm having the same problem with GTK+3 DLLs installed with MSYS2 and copied to an embedded Python 3.8 distribution.

Process Monitor shows that Python looks for libgobject-2.0-0.dll only on the PATH and never in the current directory (where the file actually is), then falls back to looking for gobject-2.0.dll in the PATH and the current directory. I know that Python 3.8 changed DLL resolution for extensions, so I even tried:

.\python.exe -c "import os; os.add_dll_directory(os.getcwd()); import weasyprint"

But that had no effect.

@kwfan
Copy link

kwfan commented Jul 27, 2020

I got to this page and fixed the problem on my side by a few trials:

The problem root cause on my end is due to multiple libgobject-2.0-0.dll on my system. On your anaconda command prompt, run "where libgobject-2.0-0.dll".

My first output is:
(base) C:\WINDOWS\system32>where libgobject-2.0-0.dll

C:\Program Files (x86)\GtkSharp\2.12\bin\libgobject-2.0-0.dll
C:\Program Files\GTK3-Runtime Win64\bin\libgobject-2.0-0.dll

Since C:\Program Files (x86)\GtkSharp\2.12\bin\ has a higher priority in path variable list, so the wrong dll was found and loaded incorrectly. To fix this, go to edit variable page and edit "path" of system variable, then make C:\Program Files\GTK3-Runtime Win64\bin\ to have a higher position.

Reopen a new command prompt. If your "where" output shows:
(base) C:\WINDOWS\system32>where libgobject-2.0-0.dll

C:\Program Files\GTK3-Runtime Win64\bin\libgobject-2.0-0.dll
C:\Program Files (x86)\GtkSharp\2.12\bin\libgobject-2.0-0.dll

Then you are all set. Hope this can help.

@liZe
Copy link
Member

liZe commented Nov 24, 2020

Closing, as following the documentation steps or the hints in these comments should be enough to solve this issue.

@ARJhe
Copy link

ARJhe commented Aug 13, 2021

After checking if installed GTK-3 and Env-Params contains "C:\Program Files\GTK3-Runtime Win64\bin" .
I still got this error.
But after check the python version I found that was the 32-bit version.

I switch to the 64-bit version. It's work now.

You have to build the project in Python 64-bit version, not 32-bit.

@clebemachado
Copy link

I was having this same error.

I managed to fix (windows) putting gtk in the path above all others (for both user and system).
As I use the virtual environment to delete and create another one, I installed or easyprint.
It worked without a hitch.

GTK - https://github.com/tschoonj/GTK-for-Windows-Runtime-Environment-Installer/releases

@Gatisvagulis
Copy link

Hi, Can you help mi with the same issue in django? I Did the same as you mentioned (windows) putting gtk in the path above all others (for both user and system). But no luck. Did you do something like this?
import os
os.add_dll_directory(r"C:\Program Files\GTK3-Runtime Win64")
from weasyprint import HTML
HTML('http://weasyprint.org/').write_pdf('/tmp/weasyprint-website.pdf')

Thanks

I was having this same error.

I managed to fix (windows) putting gtk in the path above all others (for both user and system). As I use the virtual environment to delete and create another one, I installed or easyprint. It worked without a hitch.

GTK - https://github.com/tschoonj/GTK-for-Windows-Runtime-Environment-Installer/releases

@clebemachado
Copy link

clebemachado commented Dec 15, 2021

I don't have the project anymore. But as I said, I put it above all others and reinstalled WeasyPrint.
Version gtk3 --> gtk3-runtime-3.24.29-2021-04-29-ts-win64

Screenshot_1

@liZe
Copy link
Member

liZe commented Dec 15, 2021

And if you’re interested, you can test the current beta version. With Python 3.8+, you shouldn’t have to play with the PATH anymore (see #1435 and the documentation).

@Gatisvagulis
Copy link

I don't have the project anymore. But as I said, I put it above all others and reinstalled WeasyPrint. Version gtk3 --> gtk3-runtime-3.24.29-2021-04-29-ts-win64

Screenshot_1

Yes I did the same. But still this error OSError: cannot load library 'gobject-2.0-0': error 0x7e. Additionally, ctypes.util.find_library() did not manage to locate a library called 'gobject-2.0-0'

@Gatisvagulis
Copy link

And if you’re interested, you can test the current beta version. With Python 3.8+, you shouldn’t have to play with the PATH anymore (see #1435 and the documentation).

Hi, I am using Python 3.9.5, Django==3.2.7, weasyprint==53.4 (tried lower version 53.0 but no luck), and gtk3-runtime-3.24.29-2021-04-29-ts-win64.exe. I did as @clebemachado suggested with paths. I restarted computer. Uninstall weasyprint and install from beginning. tired to set paths like this:
import os os.add_dll_directory(r"C:\\Program Files\\GTK3-Runtime Win64\bin") from weasyprint import HTML HTML('http://weasyprint.org/').write_pdf('/tmp/weasyprint-website.pdf')
What else I can tried?
And last thing I did in virtual environment: set WEASYPRINT_DLL_DIRECTORIES=C:\Program Files\GTK3-Runtime Win64\bin like path is on my computer.
Error still the same OSError: cannot load library 'gobject-2.0-0': error 0x7e. Additionally, ctypes.util.find_library() did not manage to locate a library called 'gobject-2.0-0'

@liZe
Copy link
Member

liZe commented Dec 16, 2021

What else I can tried?

Using add_dll_directory is the solution for you. You have to check twice the path (here you mix \ and \\), but it should work.

And last thing I did in virtual environment: set WEASYPRINT_DLL_DIRECTORIES=C:\Program Files\GTK3-Runtime Win64\bin like path is on my computer.

It will only work with version 54.0b1, and the default value is already set, so you don’t even have to set it manually. Installing this version will probably solve your problem (using pip install --pre weasyprint).

@Gatisvagulis
Copy link

Gatisvagulis commented Dec 16, 2021

54.0b1

Now I have weasyprint==54.0b1.
I tried the path in different syntax with r and without and looks like it find the path. Because if I write invalid path like this os.add_dll_directory("C:\Program Files\GTK3-Runtime Win64\\bins") I got different error FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\Program Files\\GTK3-Runtime Win64\\bins' I added extra s to bin.
With this os.add_dll_directory("C:\Program Files\GTK3-Runtime Win64\\bin") I am back to OSError: cannot load library 'gobject-2.0-0'
Do I need to write this syntax too HTML('http://weasyprint.org/').write_pdf('/tmp/weasyprint-website.pdf') ?
One more thing I can see I have not installed Pango ≥ 1.44.0. Do I need to install it in virtual environment for windows?

Also tried this command python -c "import os; print(*os.environ['PATH'].split(os.pathsep), sep='\n')" but there is no gtk-3 path.

`
C:\Users\LLL\Documents\1_graphicvibes_doc\projects\subscription\env\Scripts
C:\Program Files\Common Files\Oracle\Java\javapath
C:\WINDOWS\system32
C:\WINDOWS
C:\WINDOWS\System32\Wbem
C:\WINDOWS\System32\WindowsPowerShell\v1.0
C:\WINDOWS\System32\OpenSSH
C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL
C:\Program Files\Intel\Intel(R) Management Engine Components\DAL
C:\composer
C:\xampp\php
C:\Program Files\Gemalto\Classic Client\BIN
C:\Program Files (x86)\Gemalto\Classic Client\BIN
C:\Program Files (x86)\Oberthur Technologies\AWP\Dlls
C:\Program Files\Oberthur Technologies\AWP\Dlls
C:\Program Files (x86)\eParaksts Token Signing
C:\Program Files\nodejs
C:\Program Files\Java\jdk-11.0.8\bin
C:\Program Files\Git\cmd
C:\Users\LLL\scoop\shims
C:\Users\LLL\AppData\Local\Programs\Python\Python39\Scripts
C:\Users\LLL\AppData\Local\Programs\Python\Python39
C:\Users\LLL\AppData\Local\Programs\Python\Python38-32
C:\Users\LLL\AppData\Local\Microsoft\WindowsApps
C:\Users\LLL\AppData\Local\Programs\Python\Python38-32\Scripts
C:\Users\LLL\AppData\Local\Programs\Microsoft VS Code\bin
C:\php
C:\Users\LLL\AppData\Local\atom\bin
C:\Users\LLL\AppData\Local\Microsoft\WindowsApps
C:\Users\LLL\AppData\Roaming\Composer\vendor\bin
C:\Users\LLL\AppData\Roaming\npm
C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.2.3\bin

C:\Users\LLL\sqlite-tools-win32-x86-3360000
`

Thanks

@liZe
Copy link
Member

liZe commented Dec 16, 2021

I’ll try to be clear.

With Python 3.8+, for any version of WeasyPrint, PATH is not used at all to load DLL files. It’s useless to change this variable.

With Python 3.8+ and WeasyPrint 54.0b1:

So, if you have the OSError: cannot load library 'gobject-2.0-0' problem with Python 3.8+ and WeasyPrint 54.0b1, and if you carefully followed the installation steps, please open a new issue.

Do I need to write this syntax too HTML('http://weasyprint.org/').write_pdf('/tmp/weasyprint-website.pdf') ?

This code is needed if you actually want to transform weasyprint.org into a PDF. If you don’t want to write code, you can simply launch the weasyprint command in your virtual environment.

One more thing I can see I have not installed Pango ≥ 1.44.0. Do I need to install it in virtual environment for windows?

The GTK+ installer installs everything, including Pango.

@Gatisvagulis
Copy link

I’ll try to be clear.

With Python 3.8+, for any version of WeasyPrint, PATH is not used at all to load DLL files. It’s useless to change this variable.

With Python 3.8+ and WeasyPrint 54.0b1:

So, if you have the OSError: cannot load library 'gobject-2.0-0' problem with Python 3.8+ and WeasyPrint 54.0b1, and if you carefully followed the installation steps, please open a new issue.

Do I need to write this syntax too HTML('http://weasyprint.org/').write_pdf('/tmp/weasyprint-website.pdf') ?

This code is needed if you actually want to transform weasyprint.org into a PDF. If you don’t want to write code, you can simply launch the weasyprint command in your virtual environment.

One more thing I can see I have not installed Pango ≥ 1.44.0. Do I need to install it in virtual environment for windows?

The GTK+ installer installs everything, including Pango.

Did everything as you said. no luck. Opened new issue #1526

@Gatisvagulis
Copy link

Gatisvagulis commented Dec 17, 2021

Did everything as you said. no luck. Opened new issue #1526

Hi, I managed to solve the problem upgrading django version then close and reopen project. Thanks for helping.

@billwk254
Copy link

Hey guys update i had the same problem with a Django. project and deleting the virtual environment then setting up a new one worked for me

@michaelgobz
Copy link

hey, guys am facing the same challenge here I have tried all the options above but still in vain the
the error still says ctypes.util.find_library() did not manage to locate a library called 'gobject-2.0-0'
what could be the cause of this

@liZe
Copy link
Member

liZe commented Feb 21, 2023

Did you carefully follow the steps of the documentation? What’s your OS?

@michaelgobz
Copy link

@liZe
my os is win10, I have followed every step my environment variables seem fine
Screenshot (2)
my virtual environment can't seem to find the library even when I delete it and recreate it. error persists

@liZe
Copy link
Member

liZe commented Feb 24, 2023

Do you have the 64-bit version of Windows? Otherwise, I don’t know why it doesn’t work.

@michaelgobz
Copy link

I had to use win 11 64-bit to clear the error
Though am still skeptical why it never worked on win 10 64-bit

@DannyMtwenty
Copy link

@Tontyna thank you so much,,you are life saver..

@dimanskr
Copy link

I made a django website for windows 10 with the weasyprint library. Also installed GTK+ for Windows Runtime Environment (мversion 3). The same errors were thrown.
I tried to swap paths in path, updated windows to 11, but it didn't help.
Then i pasted 'sitecustomize.py' like Tontyna #971 (comment) to the virtual environment folder '/Lib/site-packages'
another error occurred: Fontconfig error: Cannot load default config file.
It turned out that the 'C:\Users<User>\anaconda3\fontconfig.dll' library is being loaded.
In the file 'sitecustomize.py' I changed the line:
os.environ['PATH'] = GTK_FOLDER + os.pathsep + os.environ.get('PATH', '')
to the line:
os.environ['PATH'] = GTK_FOLDER
and it WORKS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests