sys.path
シェルから実行しているときとWebサーバーから実行しているときの正しい設定に問題があるのかもしれません。
詳細はsys.path
こちら:sysモジュール。
インポートする前に、sys.pathに~/httpdocs/python-libraries/feedparser-4.1/
(フルパスを使用せずに)追加してみることをお勧めします。~/
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"
ちなみに、はhttpdocs
Webサーバーのドキュメントルートのようです。ライブラリをそこに置くのが最善のアイデアですか?(まあ、あなたが使うことができる唯一の場所がない限り...)
編集(一般的な注意として)
次のような構文は避けるのが最善です。
try:
something
except:
print "error"
これにより、発生した実際のエラーに関する情報はまったく得られません。モジュールをインポートしようとすると、そこにあると想定できますImportError
が、確信が持てません。
これはデバッグを本当に地獄にします。そこに行って、それをして、これのために数十時間を失いました:)
可能な限り、一度に1つの例外タイプをキャッチしてみてください。それで:
try:
import SomeModule
except ImportError:
print "SomeModule can't be imported"
トレースバックモジュールについても理解できます。これは標準ライブラリにあり、使用できるようになっています。したがって、例外処理コードは次のようになります。
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"