I’ve created a python software that should run as a service. It has a module and a script, which goes under /etc/init.d . I used distutils to package the system. Everything installs fine, but when I try to run the script from /etc/init.d, it raises the exception below:
Traceback (most recent call last):
File "/etc/init.d/printserver.py", line 11, in <module>
from myprintserver import *
File "/etc/rc.d/init.d/printserver.py", line 12, in <module>
import myprintserver.printserver
ImportError: No module named printserver
When I try to import the module through command line, it works perfectly.
[root@linuxbox Code]# python
Python 2.7.3 (default, Jul 24 2012, 10:05:38)
[GCC 4.7.0 20120507 (Red Hat 4.7.0-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from myprintserver import *
>>> from myprintserver.printserver import *
>>> daemon = PrintServer('/tmp/printer-daemon.pid','/dev/null','/tmp/dlog','/tmp/dlog')
>>>
I checked the module under site-packages and everything looks fine. I printed sys.path before the error show up and /usr/lib/python2.7/site-packages is included. sys.path printout below:
['/etc/rc.d/init.d', '/usr/lib/python2.7/site-packages/escpos-1.0-py2.7.egg', '/usr/lib/python2.7/site-packages/pyusb-1.0.0a3-py2.7.egg', '/usr/lib/python2.7/site-packages/mechanize-0.2.5-py2.7.egg', '/usr/lib/python2.7/site-packages/PIL-1.1.7-py2.7-linux-x86_64.egg', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gst-0.10', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages', '/usr/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']
The issue was caused because the script under /etc/init.d had the same name of the module. Therefore when I tried to include the module, the script itself was included .
Changing the name of the script solved the issue.
Since the script doesn’t have a class named printserver, the error was raised. This wasn’t an issue during the development because both module and script where in the same dir, and module was included first.