资讯详情

【解决方法】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 <twoerner@redhat.com> # Jiri Popelka <jpopelka@redhat.com> # # 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 

解决。

拓展

其实解决问题的方法挺简单的,但是说了很多,主要是为了引出这部分的扩展。

假如你的服务器上,一些系统级的指令突然无法执行,80%的成就是环境变化造成的。

比如:

CentOS7 中升级了 python2python3 之后,yum 指令不易使用,需要修改以下文件:

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

修改方法和上修改相应文件的第一行。

因此,本文的重点不是解决问题,而是解决问题的想法和其他类似问题的解决方案。

鼓励大家。

标签: woerner传感器vpb

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

 锐单商城 - 一站式电子元器件采购平台  

 深圳锐单电子有限公司