0

当我使用 apache 运行时,导入 python mdule 会在 django 中引发异常。相同的源代码适用于 django 开发服务器。我也可以从命令行导入模块。该模块是一个 Python SWIG 库。我在网上研究过类似的问题,但没有任何帮助(正斜杠、设置 PYTHONPATH、权限检查......)。

我确实了解在帖子末尾的打印语句中,文件名中有双斜杠,但我的理解(我可能错了)是可以的。

以下是其中一个失败的 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  

** * *** * swig_import_helper 中 D:\Program Files (x86)\Python27\lib\site-packages\QuantLib\QuantLib.py 失败的源代码:

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] ****************************  

** * ** * *** *从 Python 交互式解释器导入时,源代码中的打印语句的输出

***********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)  
****************************  
4

1 回答 1

0

Like all SWIG-generated modules, QuantLib is not a pure Python module but depends on an underlying C++ shared library (it should be called libQuantLib.so). This must be loadable by Apache; so make sure, for instance, that it's in the PATH, that ld has cached it, or that it has the needed permissions.

于 2012-08-27T07:16:48.617 回答