Skip to content
Advertisement

Creating Python daemon – ‘module’ object has no attribute ‘DaemonContext’

I’m trying to daemonize my app and am getting the error:

out:     with daemon.DaemonContext():
out: AttributeError: 'module' object has no attribute 'DaemonContext'

It looks like other people are getting this error from the module not being installed. As a newcomer to Python it’s a bit confusing that there is a daemon and python-daemon package and also there’s two ways of installing python packages (sudo apt-get install and sudo pip install). However, it seems like I have the package installed. I have Python 2.6 installed on Ubuntu 10.04. Any ideas?

It looks like I have the module installed:

# pip freeze
LEPL==5.0.0
MySQL-python==1.2.2
distribute==0.6.10
lockfile==0.8
matplotlib==0.99.1.1
numpy==1.3.0
pyparsing==1.5.2
python-apt==0.7.94.2ubuntu6.4
python-daemon==1.5.2
python-dateutil==1.4.1
pytz==2010b
rpy2==2.0.8
wsgiref==0.1.2

More evidence the module is installed:

$ python
>>> import daemon
>>> dir(daemon)
['DaemonContext', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '_copyright', '_license', '_url', '_version', 'daemon', 'version']

Advertisement

Answer

The program that produces the error is apparently using a different module named daemon. Did you perhaps call the program itself, or another module in the same directory, daemon.py?

If so, that will shadow the installed daemon module.

The solution

Rename daemon.py (and delete the daemon.pyc file that Python will have created) and try again.

If you don’t see anything shadowing daemon.py, make your application print daemon.__file__ and see where it is being imported from.

Advertisement