macOS 10.13运行virt-manager报ImportError: No module named gi求助
解决macOS 10.13 High Sierra下virt-manager报错ImportError: No module named gi
我之前在macOS High Sierra上折腾virt-manager时也碰到过完全一样的问题,结合自己的踩坑经验,给你几个针对性的解决方向:
1. 确保PyGObject(gi模块)针对Python2.7正确安装
gi是PyGObject库的一部分,在macOS上通过Homebrew安装时,需要明确指定关联Python2.7(默认可能会关联Python3):
- 先确认你已经安装了Homebrew的Python2.7:
brew install python@2 - 然后安装针对Python2.7的PyGObject3:
如果安装时提示brew install pygobject3 --with-python@2--with-python@2选项不存在,说明Homebrew新版本已移除该选项,可先卸载相关包再重新安装:
安装完成后,用brew uninstall --force pygobject3 gobject-introspection brew install pygobject3brew info pygobject3查看模块安装路径,找到对应Python2.7的site-packages目录(通常是/usr/local/lib/python2.7/site-packages)。
2. 让系统Python2.7能找到gi模块
Homebrew安装的模块不在系统默认Python的搜索路径里,需要手动添加环境变量:
export PYTHONPATH="/usr/local/lib/python2.7/site-packages:$PYTHONPATH"
你可以把这条命令添加到~/.bash_profile或者~/.zshrc中,避免每次终端启动都要重新设置。
3. 修改virt-manager启动脚本的Python路径
如果上面的步骤无效,大概率是virt-manager默认调用了系统自带的Python2.7,而系统Python无法访问Homebrew安装的模块。你可以修改启动脚本:
- 找到virt-manager的启动脚本路径,通常是
/usr/local/Cellar/virt-manager/1.5.0/libexec/share/virt-manager/virt-manager - 用文本编辑器打开它,把第一行的
#!/usr/bin/python改成Homebrew安装的Python2.7路径,比如:
你可以用#!/usr/local/bin/python2.7which python2.7命令确认准确路径。
4. 验证安装
完成上述步骤后,先在终端运行python2.7 -c "import gi; print('gi模块加载成功')",如果没有报错,再尝试启动virt-manager:
virt-manager --no-fork
内容的提问来源于stack exchange,提问作者user132017
相关产品推荐
相关产品推荐

