12

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']
Ben McCann
  • 18,548
  • 25
  • 83
  • 101
  • 6
    I also had this problem, but it turned out I had done `pip2 install daemon` when I should have done `pip2 install python-daemon`, which solved it for me. – kristianlm Jul 19 '13 at 09:51

3 Answers3

21

I run on this proglem too. If I call print daemon.__file__ it prints /usr/local/lib/python2.6/dist-packages/daemon.pyc, which is right file in wrong place, meaning that I have installed packege wrong way.

I used command "sudo pip install daemon", which installs only daemon.py file. We should use commnd "sudo pip install python-daemon", which installs whole package. After that print daemon.__file__ prints /usr/local/lib/python2.6/dist-packages/daemon/__init__.pyc, meaning that I have installed python-daemon -package, not just one python file daemon.py.

Confusing, but it was my own fault.

Remember to call "sudo pip uninstall daemon" before giving right installing command sudo pip uninstall python-daemon".

Reijo Korhonen
  • 428
  • 3
  • 7
20

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.

ndequeker
  • 7,932
  • 7
  • 61
  • 93
Thomas Wouters
  • 130,178
  • 23
  • 148
  • 122
  • 2
    Thanks! My script was named daemon.py and I didn't realize that would cause an issue. Calling "print daemon.__file__" was very helpful as well. – Ben McCann Mar 21 '12 at 00:34
3
pip uninstall daemon
pip install python-daemon
leonheess
  • 16,068
  • 14
  • 77
  • 112