Ubuntu系统安装多个Python版本后,如Python2.7、Python3.6、Python3.8,打开某些应用如gnome-terminal出现如下报错:
Traceback (most recent call last):
File "/usr/bin/gnome-terminal", line 9, in <module>
from gi.repository import GLib, Gio
File "/usr/lib/python3/dist-packages/gi/__init__.py", line 42, in <module>
from . import _gi
ImportError: cannot import name '_gi' from partially initialized module 'gi' (most likely due to a circular import) (/usr/lib/python3/dist-packages/gi/__init__.py)
之后试了如下办法未解决:(注意更改为自己的版本号,如cpython-35m,cpython-38m)
$ ls /usr/lib/python3/dist-packages/gi/
_constants.py _gobject overrides _signalhelper.py
docstring.py importer.py _propertyhelper.py types.py
_error.py __init__.py __pycache__
_gi_cairo.cpython-35m-x86_64-linux-gnu.so module.py pygtkcompat.py
_gi.cpython-35m-x86_64-linux-gnu.so _option.py repository
$ sudo cp _gi.cpython-35m-x86_64-linux-gnu.so _gi.cpython-38m-x86_64-linux-gnu.so
$ sudo cp _gi_cairo.cpython-35m-x86_64-linux-gnu.so _gi_cairo.cpython-38m-x86_64-linux-gnu.so
又尝试为python运行update-alternatives,更换默认Python版本未解决。
又重新安装python3-6和python3.8,仍未解决。
最终尝试了如下办法最终解决:
强制为python 3.8安装PyGObject:
sudo python3.8 -m pip install --ignore-installed PyGObject
安装过程中报错:
Installing build dependencies ... error
ERROR: Command errored out with exit status 1:
command: /usr/bin/python3.8 /tmp/pip-standalone-pip-j4y07fz_/__env_pip__.zip/pip install --ignore-installed --no-user --prefix /tmp/pip-build-env-pznw81vn/overlay --no-warn-script-location --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- setuptools wheel pycairo
cwd: None
Complete output (44 lines):
WARNING: The directory '/home/mi/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you should use sudo's -H flag.
Collecting setuptools
Downloading setuptools-60.5.0-py3-none-any.whl (958 kB)
Collecting wheel
Downloading wheel-0.37.1-py2.py3-none-any.whl (35 kB)
Collecting pycairo
Downloading pycairo-1.20.1.tar.gz (344 kB)
Installing build dependencies: started
Installing build dependencies: finished with status 'done'
Getting requirements to build wheel: started
Getting requirements to build wheel: finished with status 'done'
Preparing wheel metadata (pyproject.toml): started
Preparing wheel metadata (pyproject.toml): finished with status 'done'
Building wheels for collected packages: pycairo
Building wheel for pycairo (pyproject.toml): started
Building wheel for pycairo (pyproject.toml): finished with status 'error'
ERROR: Command errored out with exit status 1:
command: /usr/bin/python3.8 /tmp/tmpegfc5_3d build_wheel /tmp/tmp0xnqksl0
cwd: /tmp/pip-install-qfr4_61u/pycairo_47a2ab367641409eb9979699f5940b5f
Complete output (18 lines):
running bdist_wheel
running build
running build_py
creating build
creating build/lib.linux-x86_64-3.8
creating build/lib.linux-x86_64-3.8/cairo
copying cairo/__init__.py -> build/lib.linux-x86_64-3.8/cairo
copying cairo/__init__.pyi -> build/lib.linux-x86_64-3.8/cairo
copying cairo/py.typed -> build/lib.linux-x86_64-3.8/cairo
running build_ext
creating build/temp.linux-x86_64-3.8
creating build/temp.linux-x86_64-3.8/cairo
x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -DPYCAIRO_VERSION_MAJOR=1 -DPYCAIRO_VERSION_MINOR=20 -DPYCAIRO_VERSION_MICRO=1 -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/python3.8 -c cairo/bufferproxy.c -o build/temp.linux-x86_64-3.8/cairo/bufferproxy.o -Wall -Warray-bounds -Wcast-align -Wconversion -Wextra -Wformat=2 -Wformat-nonliteral -Wformat-security -Wimplicit-function-declaration -Winit-self -Winline -Wmissing-format-attribute -Wmissing-noreturn -Wnested-externs -Wold-style-definition -Wpacked -Wpointer-arith -Wreturn-type -Wshadow -Wsign-compare -Wstrict-aliasing -Wundef -Wunused-but-set-variable -Wswitch-default -Wno-missing-field-initializers -Wno-unused-parameter -Wno-unused-command-line-argument -fno-strict-aliasing -fvisibility=hidden -std=c99
cairo/bufferproxy.c:32:10: fatal error: Python.h: No such file or directory
#include <Python.h>
^~~~~~~~~~
compilation terminated.
error: command '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 1
----------------------------------------
ERROR: Failed building wheel for pycairo
Failed to build pycairo
ERROR: Could not build wheels for pycairo, which is required to install pyproject.toml-based projects
WARNING: You are using pip version 21.3; however, version 21.3.1 is available.
You should consider upgrading via the '/usr/bin/python3.8 -m pip install --upgrade pip' command.
之后又安装了如下库:
apt install python-dev
apt install python3-dev
apt install python3.8-dev
sudo apt-get install libgirepository1.0-dev
sudo apt-get install python-cairo
sudo apt-get install libcairo2
并执行:python3 -m pip install -U pycairo
$python3 -m pip install -U pycairo
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pycairo in /usr/lib/python3/dist-packages (1.16.2)
Collecting pycairo
Using cached pycairo-1.20.1.tar.gz (344 kB)
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing wheel metadata (pyproject.toml) ... done
Building wheels for collected packages: pycairo
Building wheel for pycairo (pyproject.toml) ... done
Created wheel for pycairo: filename=pycairo-1.20.1-cp38-cp38-linux_x86_64.whl size=254171 sha256=78e74e0f8117d55144ee7460f75a8c5b4fc363a204fa6d555452ee825177e9f4
Stored in directory: /home/mi/.cache/pip/wheels/7e/ba/dc/6688e3cf50462094cd938a481411e59ae185c73474f250fbe3
Successfully built pycairo
Installing collected packages: pycairo
Successfully installed pycairo-1.20.1
最终再次执行
sudo python3.8 -m pip install --ignore-installed PyGObject
安装成功:
Collecting PyGObject
Downloading PyGObject-3.42.0.tar.gz (716 kB)
|████████████████████████████████| 716 kB 251 kB/s
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing wheel metadata (pyproject.toml) ... done
Collecting pycairo>=1.16.0
Downloading pycairo-1.20.1.tar.gz (344 kB)
|████████████████████████████████| 344 kB 463 kB/s
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing wheel metadata (pyproject.toml) ... done
Building wheels for collected packages: PyGObject, pycairo
Building wheel for PyGObject (pyproject.toml) ... done
Created wheel for PyGObject: filename=PyGObject-3.42.0-cp38-cp38-linux_x86_64.whl size=760090 sha256=25abb6efe84d0bba98674f892d93363f26d2972c507d475affa4398f70572a87
Stored in directory: /tmp/pip-ephem-wheel-cache-om4qleoh/wheels/d7/c9/c1/b1a55bb0d533707eb1f730c8d82f9ef255c167bafc7a00824a
Building wheel for pycairo (pyproject.toml) ... done
Created wheel for pycairo: filename=pycairo-1.20.1-cp38-cp38-linux_x86_64.whl size=254223 sha256=0f2445ccb51e2d2e7e0079e3656fde046caa7408ff9ef93277f51f87a3f47745
Stored in directory: /tmp/pip-ephem-wheel-cache-om4qleoh/wheels/7e/ba/dc/6688e3cf50462094cd938a481411e59ae185c73474f250fbe3
Successfully built PyGObject pycairo
Installing collected packages: pycairo, PyGObject
Successfully installed PyGObject-3.42.0 pycairo-1.20.1
问题解决,可以正常启动应用。