tags:

views:

45

answers:

1

I have a problem with dbus and python. Running python from the command line, telling it import dbus and then systembus = dbus.SystemBus() results in no errors, nor does running a program written by a friend which also uses the exact same code. However, when running a program I'm trying to write, I get this error:

Traceback (most recent call last):
  File "dbtest.py", line 26, in <module>
    a = getDevs()
  File "dbtest.py", line 7, in getDevs
    bus = dbus.SystemBus()
AttributeError: 'module' object has no attribute 'SystemBus'

Any ideas as to what I'm doing wrong? I don't think I fully understand the error returned. The code I have so far is:

#!/usr/bin/env python
import dbus

def getDevs():
        bus = dbus.SystemBus()

if __name__ == "__main__":
        a = getDevs()
+3  A: 

The obvious problem is that when you are importing dbus, it is not getting all the methods with it.

In both your program and your friend's, do print dbus.__file__. This will show what .pyc it is using. If they are different, you are not importing the correct dbus module.

I'm going to guess that you are actually importing some random file called dbus.py in your local directory. Or, if your script name is "dbus.py", you are just importing itself and luckily python doesn't import recursively. The easiest solution in this case is to rename the offending file to something else.

orangeoctopus
Assuming this is the case (script named dbus.py), keep in mind that for this reason it's bad practice to name your script the same as a module from which you're importing. Python will always try to import from relative paths first, e.g. current working directory.
jathanism