【解决方法】CentOS7 报错 ModuleNotFoundError: No module named 'gi'

背景

新装的 CentOS7.6 环境,准备做一个测试机,因为是内网做着玩的,装完第一件事就是把防火墙关了,然后安装了其他的开发环境。

问题

今天想打开防火墙测试一个功能的时候,发现报了这样一个错:

[root@localhost ~]# firewall-cmd --state
Traceback (most recent call last):
  File "/usr/bin/firewall-cmd", line 24, in <module>
    from gi.repository import GObject
ModuleNotFoundError: No module named 'gi'

我就是想查一下防火墙现在的状态,未果。

问题分析

命令行已经提示出来了,是 /usr/bin/firewall-cmd 这个文件出了问题,而这个文件本身我并没有动过,所以基本可以肯定,是环境的问题。

打开 /usr/bin/firewall-cmd 文件看一眼:

[root@localhost ~]# vim /usr/bin/firewall-cmd

文件内容截取如下:

#!/usr/bin/python -Es
# -*- coding: utf-8 -*-
#
# Copyright (C) 2009-2016 Red Hat, Inc.
#
# Authors:
# Thomas Woerner <[email protected]>
# Jiri Popelka <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from gi.repository import GObject
import sys
sys.modules['gobject'] = GObject
...

可以看到第一行写的执行脚本使用的命令。

灵光一闪。

我把 CentOS7.6 默认自带的 Python 2.7.5 升级成了 Python 3.7.0,而上面的 /usr/bin/firewall-cmd 脚本需要由 python2 来执行。

问题分析出来了,那就开始解决。

解决方法

修改 /usr/bin/firewall-cmd 文件的第一行,使用 python2 来执行。

针对我这里,就是把第一行修改成了下面这样:

#!/usr/bin/python2 -Es

解决。

拓展

其实解决问题的方法挺简单的,但啰啰嗦嗦说了一大堆,主要是想引出这部分的扩展。

如果你的服务器上,某些系统级的指令突然没办法执行了,八成就是环境变了导致的。

比如:

CentOS7 中升级了 python2python3 之后,yum 指令就不好用了,需要修改以下几个文件:

/usr/bin/yum
/usr/bin/yum-config-manager

修改方式和上面一样,改一下对应文件的第一行。

所以,这篇文章的重点不是解决问题,而是解决问题的思路,以及由这个问题展开的其他类似问题的解决方法。

与大家共勉。

发布了118 篇原创文章 · 获赞 55 · 访问量 113万+

猜你喜欢

转载自blog.csdn.net/zhyl8157121/article/details/103242499
今日推荐