English 中文(简体)
import feedparser works via SSH, but fails when in browser
原标题:

I installed feedparser via SSH, using

$ python setup.py install --home=~/httpdocs/python-libraries/feedparser-4.1/

I did that because I don t seem to have permission to properly run python setup.py install

I am running the following python code in test.py .

print "Content-type: text/html

"
try:
    import feedparser
except:
    print "Cannot import feedparser.
"

The code runs fine when I am logged in by SSH. But when I view it in a browser, it prints

Cannot import feedparser.

Any ideas?

最佳回答

I tried both the sys.path and the $PYTHONPATH solutions, but they didn t seem to work. I didn t try adding PythonPath to httpd.conf or python.conf - that could have worked.

I ended up asking a root user to run # python setup.py install for me. That worked.

I was using Python 2.3.

问题回答

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

"
try:
    import feedparser
except:
    print "Cannot import feedparser.
"

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

" # 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

"

Sounds like a PYTHONPATH problem. Try changing the code to this and see what happens:

import sys
print "Content-type: text/html

"
try:
    print sys.path
    import feedparser
except:
    print "Cannot import feedparser.
"
    print sys.path

This probably will show that your ~/httpdocs/python-libraries/ directory is not in the path and you will need to either change sys.path or arrage for a PYTHONPATH environment variable, to correct the problem.

When you change the path, make sure that you use the full directory name, not the ~ shortcut.

Rather than catching the exception yourself, use the cgitb module just once at the top of your program, and you ll get better information about all errors.

import cgitb
cgitb.enable()




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签