导入错误:没有名为dbus的模块。

我试图更新我的Python,结果搞乱了整个系统。
我现在无法启动我的软件中心。我尝试了多种解决方法,但没有任何帮助。我的软件中心无法启动。
当我尝试启动它时,会显示以下错误:
astephen@localhost:/usr/bin$ ./software-centerGtk-Message: Failed to load module "gtk-vector-screenshot"
Traceback (most recent call last):
File "./software-center", line 36, in <module>
from softwarecenter.utils import (
File "/usr/share/software-center/softwarecenter/utils.py", line 19, in <module>
import dbus
ImportError: No module named dbus

我尝试重新安装dbus,使用以下命令:
apt-get install --reinstall dbus

但是这并没有帮助。
astephen@localhost:/usr/bin$ sudo apt-get install --reinstall dbusReading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 0 not      upgraded.
Need to get 0 B/231 kB of archives.
After this operation, 0 B of additional disk space will be used.
(Reading database ... 676272 files and directories currently installed.)
Preparing to unpack .../dbus_1.6.18-0ubuntu4.3_amd64.deb ...
Unpacking dbus (1.6.18-0ubuntu4.3) over (1.6.18-0ubuntu4.3) ...
Processing triggers for ureadahead (0.100.0-16) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Setting up dbus (1.6.18-0ubuntu4.3) ...

3我现在正在使用手机,但我非常确定你不需要(重新)安装的不是dbus,而是类似python-dbus或python3-dbus(Python绑定)。如果你使用synaptic,你可以很容易地搜索到正确的软件包。 - Jacob Vlijm
我尝试运行以下命令: sudo -h pip install dbus但是返回了以下错误信息: 找不到满足要求的版本 dbus (from versions: ) 没有找到匹配的发行版 dbus有什么想法吗?:( - Adrian O'neil Stephen
你试过 @user72216 说的吗? - edwinksl
5个回答

好的,那个问题已经被提出两年了,但这并没有阻止我继续浪费时间在同样的问题上。
pip3 install dbus-python

带来了一些问题,比如:
... 
checking python extra linking flags... -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions
  checking consistency of all components of python development environment... no
  configure: error: in `/tmp/pip-build-407jqhld/dbus-python/build/temp.linux-x86_64-3.7':
  configure: error:
    Could not link test program to Python. Maybe the main Python library has been
    installed in some non-standard library path. If so, pass it to configure,
    via the LIBS environment variable.
    Example: ./configure LIBS="-L/usr/non-standard-path/python/lib"
    ============================================================================
     ERROR!
     You probably have to install the development version of the Python package
     for your distribution.  The exact name of this package varies among them.
    ============================================================================

  See `config.log' for more details
  Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "/tmp/pip-build-407jqhld/dbus-python/setup.py", line 111, in <module>
      tests_require=['tap.py'],
    File "/home/schwaigeradm/.local/lib/python3.7/site-packages/setuptools/__init__.py", line 145, in setup
      return distutils.core.setup(**attrs)
    File "/usr/lib/python3.7/distutils/core.py", line 148, in setup
      dist.run_commands()
    File "/usr/lib/python3.7/distutils/dist.py", line 966, in run_commands
      self.run_command(cmd)
    File "/usr/lib/python3.7/distutils/dist.py", line 985, in run_command
      cmd_obj.run()
    File "/home/schwaigeradm/.local/lib/python3.7/site-packages/wheel/bdist_wheel.py", line 192, in run
      self.run_command('build')
    File "/usr/lib/python3.7/distutils/cmd.py", line 313, in run_command
      self.distribution.run_command(command)
    File "/usr/lib/python3.7/distutils/dist.py", line 985, in run_command
      cmd_obj.run()
    File "/tmp/pip-build-407jqhld/dbus-python/setup.py", line 64, in run
      cwd=builddir)
    File "/usr/lib/python3.7/subprocess.py", line 347, in check_call
      raise CalledProcessError(retcode, cmd)
  subprocess.CalledProcessError: Command '['/tmp/pip-build-407jqhld/dbus-python/configure', '--disable-maintainer-mode', 'PYTHON=/usr/bin/python3', '--prefix=/tmp/pip-build-407jqhld/dbus-python/build/temp.linux-x86_64-3.7/prefix']' returned non-zero exit status 1.

  ----------------------------------------
  Failed building wheel for dbus-python
  Running setup.py clean for dbus-python
Failed to build dbus-python

安装或重新安装我当前的任何软件都没有帮助。但是我意识到这是一个Python3.7的问题:
在Python2.7中,可以添加import dbus,但在Python3.7中却出现了相同的错误。
Traceback (most recent call last):
    import dbus
  File "/usr/lib/python3/dist-packages/dbus/__init__.py", line 77, in <module>
    import dbus.types as types
  File "/usr/lib/python3/dist-packages/dbus/types.py", line 6, in <module>
    from _dbus_bindings import (
ModuleNotFoundError: No module named '_dbus_bindings'

解决方案非常简单:
sudo apt-get install python3.7-dev
pip3 install dbus-python

1仍然出现您提到的相同错误! - vineeshvs

快速搜索显示,Python DBUS模块的Debian软件包名称为python-dbus(或python3-dbus)。为什么尽管它是software-center软件包的明确依赖项,但它却无法使用,这超出了我的理解范围。如果它们已损坏,您应该重新安装这两个软件包:
sudo apt-get install --reinstall software-center python-dbus

你必须使用pip或pip3安装dbus-python软件包:
pip3 install dbus-python

或者

pip install dbus-python

在执行上述命令之前,您可能需要使用sudo

Adrian O'neil Stephen:
我尝试运行sudo -h pip install dbus,但返回的结果是:找不到满足要求的版本dbus(来自版本:)。没有找到匹配的发行版dbus。有什么想法?:( -
进一步了解您所做的操作将会很有帮助,然而,我的猜测是:
考虑升级到Python3的问题
当升级到Python3时,模块名称有时会更改/导入过程也会更改;您是否手动删除了旧的Python环境或从Git安装了某些内容?
尝试aptitude 安装aptitude,它会推荐解决依赖关系的方法。但请注意,始终检查它想要执行的操作,这种更改可能会影响到其他内容,并且记下您所做的操作以及aptitude所做的操作,以便您随时可以撤消可能的错误。
重新安装系统
有时候最好的方法就是彻底清除一切(在备份所有东西后,最好使用应用程序导出功能备份要备份的应用程序,这将让事情变得更加容易)。

如果你的python3模块找不到正确版本的包,一个可能的解决办法是创建旧版本的符号链接。 以dbus为例,你可以按照以下步骤操作(假设你刚刚将python3.5升级到python3.6): $ cd /usr/lib/python3/dist-packages/ $ ln -s _dbus_glib_bindings.cpython-{35m,36m}-x86_64-linux-gnu.so 如果你遇到进一步的错误,请尝试链接所有的包。
a=$(find /usr/lib/python3/dist-packages -name '*35m*so')
b=$(echo $a | tr 35m 36m)
IFS=' ' read -r -a a <<< $a
IFS=' ' read -r -a b <<< $b

for ((i=0;i<${#a[@]};++i)); do
    ln -s "${a[i]}" "${b[i]}"
done