2012-08-25 118 views
0

當我使用apache運行時,導入python mdule在django中拋出異常。相同的源代碼在django開發服務器上正常工作。我也可以從命令行導入模塊。該模塊是一個Python SWIG庫。我在網上研究過類似的問題,但沒有任何幫助(正斜線,設置PYTHONPATH,權限檢查...)。SWIG python模塊的導入失敗apache

我明白在文章末尾的打印語句中文件名中有雙斜槓,但我的理解(我可能錯了)是這樣。

這裏有3個方案,其中一個出現故障:

在命令行中我可以執行以下工作得很好:

import QuantLib 

使用Django開發服務器,我可以運行下面的代碼在我看來,沒有任何錯誤:

from django.http import HttpResponse 
import sys 
import QuantLib 
def home(request): 
    return HttpResponse("This is a test.") 

現在,如果我移動與下面的腳本我使用與上述同樣的觀點得到以下錯誤到Apache。該文件是django_wsgi:

import os, sys 
os.environ['DJANGO_SETTINGS_MODULE'] = 'tgVAR.settings' 
import django.core.handlers.wsgi 
application = django.core.handlers.wsgi.WSGIHandler() 

** * ** *錯誤的瀏覽器

ImportError at/ 
DLL load failed: The specified module could not be found. 
Request Method:  GET 
Request URL: (here is the url) 
Django Version:  1.4 
Exception Type:  ImportError 
Exception Value: DLL load failed: The specified module could not be found. 

Exception Location:  D:\Program Files (x86)\Python27\lib\site-packages\QuantLib\QuantLib.py in swig_import_helper, line 29 
Python Executable: D:\Program Files (x86)\Apache Software Foundation\Apache2.2\bin\httpd.exe 
Python Version:  2.7.2 
Python Path:  

['D:\\Program Files (x86)\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg', 
'C:\\Windows\\system32\\python27.zip', 
'D:\\Program Files (x86)\\Python27\\Lib', 
'D:\\Program Files (x86)\\Python27\\DLLs', 
'D:\\Program Files (x86)\\Python27\\Lib\\lib-tk', 
'D:\\Program Files (x86)\\Apache Software Foundation\\Apache2.2', 
'D:\\Program Files (x86)\\Apache Software Foundation\\Apache2.2\\bin', 
'D:\\Program Files (x86)\\Python27', 
'D:\\Program Files (x86)\\Python27\\lib\\site-packages', 
'D:\\Program Files (x86)\\Python27\\Lib\\site-packages\\QuantLib', 
'D:\\Program Files (x86)\\Django-1.4', 
'D:\\Home', 
'D:\\Home\\tgVAR', 
'c:/Home/test1/test1'] 

Server time: Sat, 25 Aug 2012 14:02:27 -0400 

** * *** *的源代碼FAILS D:\ Program Files(x86)\ Python27 \ lib \ site-packages \ QuantLib \ QuantLib。PY在swig_import_helper:

from sys import version_info 
if version_info >= (2,6,0): 
    def swig_import_helper(): 
     from os.path import dirname 
     import imp 
     fp = None 
     try: 
      fp, pathname, description = imp.find_module('_QuantLib', [dirname(__file__)]) 
     except ImportError: 
      import _QuantLib 
      return _QuantLib 
     if fp is not None: 
      try:  
       #TG start 
       print '***********TG***************' 
       print fp 
       print pathname 
       print description 
       print '****************************' 
      #TG end 
       _mod = imp.load_module('_QuantLib', fp, pathname, description) 
      finally: 
       fp.close() 
      return _mod 
    _QuantLib = swig_import_helper() 
    del swig_import_helper 
else: 
    import _QuantLib 
del version_info 

** * ** * *** *局部變量APACHE何時拋出異常:

Variable Value 
fp <closed file 'D:\Program Files (x86)\Python27\lib\site-packages\QuantLib\_QuantLib.pyd', mode 'rb' at 0x0429C5A0> 
imp <module 'imp' (built-in)> 
dirname <function dirname at 0x018F29B0> 
pathname 'D:\\Program Files (x86)\\Python27\\lib\\site-packages\\QuantLib\\_QuantLib.pyd' 
description ('.pyd', 'rb', 3) 

** * ** * *** *輸出源代碼打印報表時運行Apache

[Sat Aug 25 14:02:26 2012] [error] ***********TG*************** 
[Sat Aug 25 14:02:26 2012] [error] <open file 'D:\\Program Files (x86)\\Python27\\lib\\site-packages\\QuantLib\\_QuantLib.pyd', mode 'rb' at 0x0429C5A0> 
[Sat Aug 25 14:02:26 2012] [error] D:\\Program Files (x86)\\Python27\\lib\\site-packages\\QuantLib\\_QuantLib.pyd 
[Sat Aug 25 14:02:26 2012] [error] ('.pyd', 'rb', 3) 
[Sat Aug 25 14:02:26 2012] [error] **************************** 

** * ** * *** *輸出在源代碼中的打印語句在做進口蟒蛇交互式解釋

***********TG*************** 
<open file 'D:\Program Files (x86)\Python27\Lib\site-packages\QuantLib\_QuantLib.pyd', mode 'rb' at 0x02879CD8> 
D:\Program Files (x86)\Python27\Lib\site-packages\QuantLib\_QuantLib.pyd 
('.pyd', 'rb', 3) 
**************************** 

** * ** * *** *輸出在源代碼中的打印語句當運行Django開發服務器

***********TG*************** 
<open file 'D:\Program Files (x86)\Python27\Lib\site-packages\QuantLib\_QuantLib.pyd', mode 'rb' at 0x0312AAC8> 
D:\Program Files (x86)\Python27\Lib\site-packages\QuantLib\_QuantLib.pyd 
('.pyd', 'rb', 3) 
**************************** 

回答

0

像所有SWIG生成的模塊,QuantLib不是純Python模塊,而是取決於一個基礎C++上共享庫(它應該被稱爲libQuantLib.so)。這必須由Apache加載;所以請確保,例如,它在PATH中,ld已經緩存了它,或者它具有所需的權限。

+0

請您詳細說明「該ld已緩存它」。我不確定你的意思。我能夠從python交互式解釋器以及django開發服務器中導入QuantLib。所以,一定有一些特定於apache的東西。 –

+0

我的意思是確保您將路徑添加到/etc/ld.so.conf,並且如果系統需要它,則運行ldconfig。您可能也需要設置LD_LIBRARY_PATH。 –