Python – How to use Python Dbus binding (bind) in Anaconda

How to use Python Dbus binding (bind) in Anaconda… here is a solution to the problem.

How to use Python Dbus binding (bind) in Anaconda

I’m trying to install dbus in an Anaconda python environment, but I’m struggling.

This is the error message I received:

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
>>> 

Here are some outputs that I think might be asked:

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 works well on system python, but not on 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
>>> 

Can anyone help me? Am I missing something obvious?

Thanks in advance.

Solution

I have a similar issue, very few dbus and python don’t work well out-of-the-box. i.e. apt-get) to make dbus work. I believe the /home/e/anaconda3/lib/python3.5/site-packages/_dbus_bindings.so: undefined symbol: _Py_ZeroStruct error you see is directly related to this.

Instead of adding anything to ~/anaconda3/lib/python3.6/site-packages, conda install dbus seems to have installed some executables in ~/anaconda3/bin/ like dbus-run-session , dbus-daemon and others. This makes some sense when you analyze the contents of dbus tarball< a href="https://anaconda.org/conda-forge/dbus" rel="noreferrer noopener nofollow">https://anaconda.org/conda-forge/dbus because it’s both C files and executables. I’m not sure it should be a dbus python module, but I could be wrong.

Edit:

I searched the conda repository and found that some people uploaded a version of dbus-python, presumably compiled and installed by them. I’ve tried this one outputs in a py3.6 conda environment via:

conda install -c scottwales dbus-python

Then I can import dbus. This is a hacky method that should not be used in production, I recommend listening to it
Below is a post by Carlos Cordoba. However, if you need a solution now, search for some user conda packages or try compiling the library yourself.

Related Problems and Solutions