pygobject / pycairo

Python bindings for cairo
https://pycairo.readthedocs.io
Other
622 stars 85 forks source link

import error on windows - py3.8 #182

Open DjLegolas opened 4 years ago

DjLegolas commented 4 years ago

I'm using Windows 10, with Python 3.8.2 64bit. I managed to install the package successfully but when trying to import is I get the following error:

Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cairo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python38\lib\site-packages\pycairo-1.19.1-py3.8-win-amd64.egg\cairo\__init__.py", line 1, in <module>
    from ._cairo import *  # noqa: F401,F403
ImportError: DLL load failed while importing _cairo: The specified module could not be found.

This is a new behavior from python3.8 on windows: https://docs.python.org/3.8/whatsnew/3.8.html#bpo-36085-whatsnew https://docs.python.org/3.8/library/os.html#os.add_dll_directory

lazka commented 4 years ago

Yeah, starting with 3.8 you need to use add_dll_directory() to tell python where your cairo.dll is. This is not something pycairo can do.

In case you are using one of the prebuilt pycairo packages provided on other websites which include cairo then they need to call add_dll_directory() with the directory cairo is in.

As a hacky workaround you can make Python behave like before using: https://github.com/pygobject/pycairo/blob/8c5d0c700cb4b49bff1092dca498ef3e75439a26/tests/__init__.py#L6 but that only works if you don't dynamically update PATH.

mhertz commented 4 years ago

I'm sorry, probably off-topic, but have same issue as OP. I downloaded a wheel for latest pycairo, and when installed from pip then it works fine importing, even as py3.8 wheel and python. However, when building it using gvsbuild, same version, then the error comes up when trying start it manually, like OP. The downloaded package has a Cairo dll in Cairo install dir under site-packages/Cairo*, but the one I build with gvsbuild, hasent any dll there, and just has instead a _Cairo.py and fails with OP message. So it seems it can find dll if next to built pyd, but if not will fail. I posted ticket at gvsbuild with log here: https://github.com/wingtk/gvsbuild/issues/350

I'm trying to figure out why no dll built and a _Cairo.py left instead.

Sorry again if completely off-topic.

Edit: Using py3.77 then with gvsbuild there is a dll generated next to the pyd, like the downloaded py38 wheel version.

lazka commented 4 years ago

We don't provide wheels, so not sure.

DjLegolas commented 4 years ago

@lazka - I added to PATH the location of the cairo library and added set_dll_search_path() to the init of pycairo and now it works.

But this solution is, as you wrote, hacky. Do you think there is a way for pycairo to overcome this? or the recommended solution would be install a third-party pre built/compiled wheel?

lazka commented 4 years ago

My auto converting PATH to set_dll_search_path() ist hacky. set_dll_search_path() alone isn't, and it's what you are supposed to use with Python 3.8.

iamsaurabhgupt commented 4 years ago

Yeah, starting with 3.8 you need to use add_dll_directory() to tell python where your cairo.dll is. This is not something pycairo can do.

In case you are using one of the prebuilt pycairo packages provided on other websites which include cairo then they need to call add_dll_directory() with the directory cairo is in.

As a hacky workaround you can make Python behave like before using:

https://github.com/pygobject/pycairo/blob/8c5d0c700cb4b49bff1092dca498ef3e75439a26/tests/__init__.py#L6

but that only works if you don't dynamically update PATH.

I had been trying to fix this for hours. Added GTK+, MSYS2, Visual Studio C Compiler and Uniconverter. But, nothing seemed to work. Finally, got it working after putting the script to call add_dll_directory. Thanks a lot @lazka Still, I feel the windows wheel waiting for merge on #191 will make life a lot easier.

EriKWDev commented 4 years ago

Everybody, bring some love to #191!

stuaxo commented 4 years ago

@DjLegolas can you try the latest release ?

DjLegolas commented 3 years ago

@stuaxo - sorry for the late reply. Well, when building it myself I'm still getting the same error... But when installing it using pip install pycairo, it installs successfully and I can use it without error 👍