Maybe it's the problem with setting correct sys.path
while running from shell vs from web server.
More about sys.path
here: sys module.
I'd recomend to try adding ~/httpdocs/python-libraries/feedparser-4.1/
(best using full path, without ~/
) to your sys.path before the import.
import sys
sys.path.append('/home/user/httpdocs/python-libraries/feedparser-4.1/')
print "Content-type: text/html\n\n"
try:
import feedparser
except:
print "Cannot import feedparser.\n"
Oh, and by the way, the httpdocs
seems like a document root for your web server. Is it the best idea to put the library there? (well, unless there's the only place you can use...)
edit (as a general note)
It's best to avoid the syntax like:
try:
something
except:
print "error"
This gives you absolutly no information about the actual error you encounter. You can assume that if you try to import a module, you have ImportError
there, but can't be sure.
This makes debugging a real hell. Been there, done that, have lost dozens of hours due to this :)
Whenever you can, try catching one exception type at a time. So:
try:
import SomeModule
except ImportError:
print "SomeModule can't be imported"
You can also get familiar with the traceback module. It's in the standard library and it's there so you can use it. So, your exception handling code could be something like this:
sys.path.append('/home/user/httpdocs/python-libraries/feedparser-4.1/')
try:
import feedparser
except ImportError:
print "Content-type: text/plain\n\n" # text/plain so we get the stacktrace printed well
import traceback
import sys
traceback.print_exc(sys.stdout) # default is sys.stderr, which is error log in case of web server running your script, we want it on standart output
sys.exit(1)
# here goes your code to execute when all is ok, including:
print "Content-type: text/html\n\n"