Skip to main content
 首页 » 编程设计

python之如何在 Anaconda 中使用 Python Dbus 绑定(bind)

2025年01月19日11落叶无声

我正在尝试在 Anaconda python 环境中安装 dbus,但我很挣扎。

这是我收到的错误消息:

e@gateway:~$ python 
Python 3.5.4 |Anaconda custom (64-bit)| (default, Oct 13 2017, 11:22:58)  
[GCC 7.2.0] on linux 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import dbus 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
  File "/home/e/anaconda3/lib/python3.5/site-packages/dbus/__init__.py", line 77, in <module> 
    import dbus.types as types 
  File "/home/e/anaconda3/lib/python3.5/site-packages/dbus/types.py", line 6, in <module> 
    from _dbus_bindings import ( 
ImportError: /home/e/anaconda3/lib/python3.5/site-packages/_dbus_bindings.so: undefined symbol: _Py_ZeroStruct 
>>>  

以下是我认为可能会被问到的一些输出:

e@gateway:~$ conda install dbus 
Fetching package metadata ........... 
Solving package specifications: . 
 
# All requested packages already installed. 
# packages in environment at /home/e/anaconda3: 
# 
dbus                      1.10.22              h3b5a359_0   
 
 
e@gateway:~$ sudo apt-get install libdbus-glib-1-dev libdbus-1-dev 
Reading package lists... Done 
Building dependency tree        
Reading state information... Done 
libdbus-glib-1-dev is already the newest version (0.106-1). 
libdbus-1-dev is already the newest version (1.10.6-1ubuntu3.3). 
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. 
 
 
e@gateway:~$ sudo apt-get install dbus 
Reading package lists... Done 
Building dependency tree        
Reading state information... Done 
dbus is already the newest version (1.10.6-1ubuntu3.3). 
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. 
 
 
e@gateway:~$ which python 
/home/e/anaconda3/bin/python 
 
 
e@gateway:~$ conda --version 
conda 4.3.31 
 
e@gateway:~$ sudo /home/e/anaconda3/bin/python -m pip install dbus-python 
The directory '/home/e/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. 
The directory '/home/e/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. 
Requirement already satisfied: dbus-python in ./anaconda3/lib/python3.5/site-packages 

DBus 在系统 python 上运行良好,但不适用于 Anaconda Python。

python 2.7:

e@gateway:~$ which python 
/usr/bin/python 
e@gateway:~$ python 
Python 2.7.12 (default, Nov 20 2017, 18:23:56)  
[GCC 5.4.0 20160609] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import dbus 
>>>  

python 3.5:

e@gateway:~$ which python3 
/usr/bin/python3 
e@gateway:~$ python3 
Python 3.5.2 (default, Nov 23 2017, 16:37:01)  
[GCC 5.4.0 20160609] on linux 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import dbus 
>>>  

谁能帮帮我?我是否遗漏了一些明显的东西?

提前致谢。

请您参考如下方法:

我有类似的问题,很少有 dbus 和 python don't work well out-of-the-box .共识似乎是您需要系统级安装(即 apt-get)才能让 dbus 工作。我相信您看到的 /home/e/anaconda3/lib/python3.5/site-packages/_dbus_bindings.so: undefined symbol: _Py_ZeroStruct 错误与此直接相关。

conda install dbus 没有向 ~/anaconda3/lib/python3.6/site-packages 添加任何内容,而是似乎在 中安装了一些可执行文件~/anaconda3/bin/ like dbus-run-session, dbus-daemon 等。当你分析dbus tarball的内容时,这有一些意义https://anaconda.org/conda-forge/dbus ,因为它都是 C 文件和可执行文件。我不确定它应该是 dbus python 模块,但我可能是错的。

编辑:

我搜索了 conda 存储库,发现一些人上传了一个版本的 dbus-python,大概是他们编译和安装的。我试过 this one通过以下方式在 py3.6 conda 环境中输出:

conda install -c scottwales dbus-python

然后我可以导入 dbus。这是一种 hacky 方法,不应该在生产中使用,我建议听听 下面是 Carlos Cordoba 的帖子。但是,如果您现在需要解决方案,请搜索一些用户 conda 包或尝试自己编译库。