解决更换python版本出现 ModuleNotFoundError: No module named 'apt_pkg'等错误

系统环境

  1. Ubuntu18.04
  2. 系统自带python2.7和python3.6;

在安装python3.7.3后,把默认的python和python3都指向了python3.7,使用更新命令apt-get update后出现类似以下错误:

Traceback (most recent call last):
  File "/usr/lib/cnf-update-db", line 8, in <module>
    from CommandNotFound.db.creator import DbCreator
  File "/usr/local/python/lib/python3.7/CommandNotFound/db/creator.py", line 11, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Reading package lists... Done
E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/lib/command-not-found/ -a -e /usr/lib/cnf-update-db; then /usr/lib/cnf-update-db > /dev/null; fi'
E: Sub-process returned an error code

而且不能打开终端,只有在目录下右键打开

解决方法

如果你曾经通过源码安装最新的python 例如:

./configure --prefix=/usr/local/python

那么你的python安装目录就在/usr/local/python

通过以下操作可以解决问题

apt-get remove --purge python-apt 
apt-get install -f -y python-apt

进入以下目录,可以看到.so动态链接库的命名中含有36字样,因为系统默认python3版本为python3.6。

cd /usr/lib/python3/dist-packages/

将所有的动态链接库文件复制并重命名

cp apt_inst.cpython-36m-x86_64-linux-gnu.so ./apt_inst.cpython-37m-x86_64-linux-gnu.so
cp apt_pkg.cpython-36m-x86_64-linux-gnu.so ./apt_pkg.cpython-37m-x86_64-linux-gnu.so
cp brlapi.cpython-36m-x86_64-linux-gnu.so ./brlapi.cpython-37m-x86_64-linux-gnu.so
cp _cffi_backend.cpython-36m-x86_64-linux-gnu.so ./_cffi_backend.cpython-37m-x86_64-linux-gnu.so
cp cups.cpython-36m-x86_64-linux-gnu.so ./cups.cpython-37m-x86_64-linux-gnu.so
cp cupsext.cpython-36m-x86_64-linux-gnu.so ./cupsext.cpython-37m-x86_64-linux-gnu.so
cp _dbus_bindings.cpython-36m-x86_64-linux-gnu.so ./_dbus_bindings.cpython-37m-x86_64-linux-gnu.so
cp _dbus_glib_bindings.cpython-36m-x86_64-linux-gnu.so ./_dbus_glib_bindings.cpython-37m-x86_64-linux-gnu.so
cp hpmudext.cpython-36m-x86_64-linux-gnu.so ./hpmudext.cpython-37m-x86_64-linux-gnu.so
cp netifaces.cpython-36m-x86_64-linux-gnu.so ./netifaces.cpython-37m-x86_64-linux-gnu.so
cp pcardext.cpython-36m-x86_64-linux-gnu.so ./pcardext.cpython-37m-x86_64-linux-gnu.so
cp scanext.cpython-36m-x86_64-linux-gnu.so ./scanext.cpython-37m-x86_64-linux-gnu.so
cp _yaml.cpython-36m-x86_64-linux-gnu.so ./_yaml.cpython-37m-x86_64-linux-gnu.so

将动态链接库复制到python3.7安装目录

cp *37*.so /usr/local/python/lib/python3.7/

以防出现其他错误,最后再加上下面这两句

cp -r /usr/lib/python3/dist-packages/CommandNotFound /usr/local/python/lib/python3.7/
cp /usr/lib/python3/dist-packages/lsb_release.py /usr/local/python/lib/python3.7/

测试不再报错

sudo apt-get update

Ubuntu Ctrl+Alt+T无法打开控制台修复

这时Ctrl+Alt+T还是无法打开控制台,在任意目录右键打开控制台,输入以下命令

gnome-terminal

出现以下报错信息,显示没有gi模块

Traceback (most recent call last):
  File "/usr/bin/gnome-terminal", line 9, in <module>
    from gi.repository import GLib, Gio
ModuleNotFoundError: No module named 'gi'

和上面步骤很像,类似地,把gi包复制到python3.7安装目录

cd /usr/lib/python3/dist-packages/
sudo cp -rf gi /usr/local/python/lib/python3.7/

把里面的.so文件改成python3.7的

cd /usr/local/python/lib/python3.7/gi
sudo mv _gi.cpython-36m-x86_64-linux-gnu.so  _gi.cpython-37m-x86_64-linux-gnu.so
sudo mv _gi_cairo.cpython-36m-x86_64-linux-gnu.so  _gi_cairo.cpython-37m-x86_64-linux-gnu.so

测试控制台可以正常打开

gnome-terminal

会不会以后遇到类似的错误都要复制包0.0

你可能感兴趣的:(python安装)