python-3.xpygtkpygobjectpgicefpython

PyGTK, PyGObject and Pgi in python3


In https://github.com/cztomczak/cefpython/blob/master/src/linux/binaries_64bit/kivy_.py, pygtk and gtk are used. (Note that the source code is tested in Linux only, and I am using Windows)

When I tried to pip install pygtk, there was an error

Collecting pygtk
  Using cached pygtk-2.24.0.tar.bz2 (2.4 MB)
    ERROR: Command errored out with exit status 1:
     command: 'c:\users\jared\appdata\local\programs\python\python37\python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\jared\\AppData\\Local\\Temp\\pip-install-rle_vbsd\\pygtk\\setup.py'"'"'; __file__='"'"'C:\\Users\\jared\\AppData\\Local\\Temp\\pip-install-rle_vbsd\\pygtk\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\jared\AppData\Local\Temp\pip-pip-egg-info-pex79haa'
         cwd: C:\Users\jared\AppData\Local\Temp\pip-install-rle_vbsd\pygtk\
    Complete output (1 lines):
    ERROR: Could not import dsextras module: Make sure you have installed pygobject.
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

Thus, I went to pip install pygobject, error again.

Collecting pygobject
  Using cached PyGObject-3.38.0.tar.gz (712 kB)
  Installing build dependencies ... error
  ERROR: Command errored out with exit status 1:
   command: 'c:\users\jared\appdata\local\programs\python\python37\python.exe' 'c:\users\jared\appdata\local\programs\python\python37\lib\site-packages\pip' install --ignore-installed --no-user --prefix 'C:\Users\jared\AppData\Local\Temp\pip-build-env-t_0q3g_h\overlay' --no-warn-script-location --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- setuptools wheel pycairo
       cwd: None
  Complete output (34 lines):
  Collecting setuptools
    Using cached setuptools-50.3.0-py3-none-any.whl (785 kB)
  Collecting wheel
    Using cached wheel-0.35.1-py2.py3-none-any.whl (33 kB)
  Collecting pycairo
    Using cached pycairo-1.19.1.tar.gz (205 kB)
  Using legacy 'setup.py install' for pycairo, since package 'wheel' is not installed.
  Installing collected packages: setuptools, wheel, pycairo
      Running setup.py install for pycairo: started
      Running setup.py install for pycairo: finished with status 'error'
      ERROR: Command errored out with exit status 1:
       command: 'c:\users\jared\appdata\local\programs\python\python37\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\jared\\AppData\\Local\\Temp\\pip-install-faiuku8y\\pycairo\\setup.py'"'"'; __file__='"'"'C:\\Users\\jared\\AppData\\Local\\Temp\\pip-install-faiuku8y\\pycairo\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\jared\AppData\Local\Temp\pip-record-kq9tlvv8\install-record.txt' --single-version-externally-managed --prefix 'C:\Users\jared\AppData\Local\Temp\pip-build-env-t_0q3g_h\overlay' --compile --install-headers 'C:\Users\jared\AppData\Local\Temp\pip-build-env-t_0q3g_h\overlay\Include\pycairo'
           cwd: C:\Users\jared\AppData\Local\Temp\pip-install-faiuku8y\pycairo\
      Complete output (18 lines):
      running install
      running build
      running build_py
      creating build
      creating build\lib.win-amd64-3.7
      creating build\lib.win-amd64-3.7\cairo
      copying cairo\__init__.py -> build\lib.win-amd64-3.7\cairo
      copying cairo\__init__.pyi -> build\lib.win-amd64-3.7\cairo
      copying cairo\py.typed -> build\lib.win-amd64-3.7\cairo
      running build_ext
      building 'cairo._cairo' extension
      creating build\temp.win-amd64-3.7
      creating build\temp.win-amd64-3.7\Release
      creating build\temp.win-amd64-3.7\Release\cairo
      C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.27.29110\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -DPYCAIRO_VERSION_MAJOR=1 -DPYCAIRO_VERSION_MINOR=19 -DPYCAIRO_VERSION_MICRO=1 -Ic:\users\jared\appdata\local\programs\python\python37\include -Ic:\users\jared\appdata\local\programs\python\python37\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.27.29110\include" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt" /Tccairo/device.c /Fobuild\temp.win-amd64-3.7\Release\cairo/device.obj
      device.c
      C:\Users\jared\AppData\Local\Temp\pip-install-faiuku8y\pycairo\cairo\pycairo.h(37): fatal error C1083: Cannot open include file: 'cairo.h': No such file or directory
      error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Tools\\MSVC\\14.27.29110\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2
      ----------------------------------------
  ERROR: Command errored out with exit status 1: 'c:\users\jared\appdata\local\programs\python\python37\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\jared\\AppData\\Local\\Temp\\pip-install-faiuku8y\\pycairo\\setup.py'"'"'; __file__='"'"'C:\\Users\\jared\\AppData\\Local\\Temp\\pip-install-faiuku8y\\pycairo\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\jared\AppData\Local\Temp\pip-record-kq9tlvv8\install-record.txt' --single-version-externally-managed --prefix 'C:\Users\jared\AppData\Local\Temp\pip-build-env-t_0q3g_h\overlay' --compile --install-headers 'C:\Users\jared\AppData\Local\Temp\pip-build-env-t_0q3g_h\overlay\Include\pycairo' Check the logs for full command output.
  ----------------------------------------
ERROR: Command errored out with exit status 1: 'c:\users\jared\appdata\local\programs\python\python37\python.exe' 'c:\users\jared\appdata\local\programs\python\python37\lib\site-packages\pip' install --ignore-installed --no-user --prefix 'C:\Users\jared\AppData\Local\Temp\pip-build-env-t_0q3g_h\overlay' --no-warn-script-location --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- setuptools wheel pycairo Check the logs for full command output.

I then referred to some posts on stackoverflow, they told me that gtk is no longer needed in python3 and replaced it by pgi. (I don't know should I replace the pygtk or gtk in the source code) So I tried out pip install pgi, error again.

Collecting pgi
  Using cached pgi-0.0.11.2.tar.gz (239 kB)
    ERROR: Command errored out with exit status 1:
     command: 'c:\users\jared\appdata\local\programs\python\python37\python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\jared\\AppData\\Local\\Temp\\pip-install-2znm3fjc\\pgi\\setup.py'"'"'; __file__='"'"'C:\\Users\\jared\\AppData\\Local\\Temp\\pip-install-2znm3fjc\\pgi\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\jared\AppData\Local\Temp\pip-pip-egg-info-5e57ceft'
         cwd: C:\Users\jared\AppData\Local\Temp\pip-install-2znm3fjc\pgi\
    Complete output (21 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\jared\AppData\Local\Temp\pip-install-2znm3fjc\pgi\setup.py", line 18, in <module>
        import pgi
      File "C:\Users\jared\AppData\Local\Temp\pip-install-2znm3fjc\pgi\pgi\__init__.py", line 10, in <module>
        from .importer import require_version, get_required_version
      File "C:\Users\jared\AppData\Local\Temp\pip-install-2znm3fjc\pgi\pgi\importer.py", line 15, in <module>
        from .clib.gir import GIRepository
      File "C:\Users\jared\AppData\Local\Temp\pip-install-2znm3fjc\pgi\pgi\clib\gir\__init__.py", line 8, in <module>
        from .giargument import GIArgument
      File "C:\Users\jared\AppData\Local\Temp\pip-install-2znm3fjc\pgi\pgi\clib\gir\giargument.py", line 9, in <module>
        from ..glib import gboolean, gint8, guint8, gint16, guint16, gint32, guint32
      File "C:\Users\jared\AppData\Local\Temp\pip-install-2znm3fjc\pgi\pgi\clib\glib.py", line 16, in <module>
        _glib = find_library("glib-2.0")
      File "C:\Users\jared\AppData\Local\Temp\pip-install-2znm3fjc\pgi\pgi\clib\_utils.py", line 99, in find_library
        _internal[name] = cdll.LoadLibrary(_so_mapping[name])
      File "c:\users\jared\appdata\local\programs\python\python37\lib\ctypes\__init__.py", line 442, in LoadLibrary
        return self._dlltype(name)
      File "c:\users\jared\appdata\local\programs\python\python37\lib\ctypes\__init__.py", line 364, in __init__
        self._handle = _dlopen(self._name, mode)
    OSError: [WinError 126] The specified module could not be found
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

I don't know what was happening. I hope somebody will pull me out from this infinite error loop. Lots of thanks.


Solution

  • first I want to mention that I am using Xubuntu 20.04, so Linux.

    In order to get the kivy example working you will need to replace the pygtk module with the PyGobject one. So install that, here are the instructions:

    https://pygobject.readthedocs.io/en/latest/getting_started.html#windows-logo-windows

    Now in this file https://github.com/cztomczak/cefpython/blob/master/src/linux/binaries_64bit/kivy_.py you need to change the imports and some other stuff. Here is how my imports look like

    ...
    
    from cefpython3 import cefpython as cef
    
    import gi       # notice this line is added
    # import pygtk  # this one is commented out or you can delete it
    from gi.repository import Gtk as gtk    # this one is added
    # import gtk    # this one is commented out or you can delete it
    import sys
    
    ...
    
    from kivy.graphics.texture import Texture
    from kivy.uix.boxlayout import BoxLayout
    from kivy.base import EventLoop
    
    # Global variables
    g_switches = None
    
    # PyGTK required
    # pygtk.require('2.0') # this one is commented out or you can delete it
    
    
    class BrowserLayout(BoxLayout):
    ...
    

    I also needed to fix a parameter passed to the windowInfo.SetAsOffscreen method called somewhere around line 208 (in my case), look for the start_cef method definition. I set the parameter to 0 as per this https://github.com/cztomczak/cefpython/blob/master/api/WindowInfo.md#setasoffscreen as it mentions:

    You can pass 0 as parentWindowHandle, but then some things like context menus and plugins may not display correctly.

    So the code looks like this:

    ...
    #popup widgets to work.
    gtkwin = gtk.Window()
    gtkwin.realize()
    
    # WindowInfo offscreen flag
    windowInfo = cef.WindowInfo()
    windowInfo.SetAsOffscreen(0)
        
    # Create Broswer and naviagte to empty page <= OnPaint won't get
    # called yet
    ...