0

I've got a Python script which I'm running as a CGI script under Apache. The script calls a module which successfully imports without errors normally, however when the script is run by Apache, it sometimes works fine and it sometimes results in an ImportError ("No module named MeCab"). I have no idea what could be causing this to fail intermittently. Any ideas?

(I'm running Python 2.7.3 and Apache 2.2.22 on Ubuntu 12.04)

nedned
  • 3,552
  • 8
  • 38
  • 41

1 Answers1

0

So it turns out that the module's directory just wasn't in the PYTHONPATH. Which is strange because this should have meant it failed always (rather than intermittently) and because this machine was already running as a webserver and for some reason did not have Apache configured to have /usr/lib/python2.7/dist-packages/ in the PYTHONPATH like you would expect.

An easy way to fix this is to append the directory to the path within the Python script:

import sys
sys.path.append('/usr/lib/python2.7/dist-packages/')
nedned
  • 3,552
  • 8
  • 38
  • 41