【问题标题】:Import of SWIG python module fails with apache使用 apache 导入 SWIG python 模块失败
【发布时间】:2012-08-25 18:47:00
【问题描述】:

当我使用 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  

*******失败的源代码 D:\Program Files (x86)\Python27\lib\site- swig_import_helper 中的 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)  
****************************  

【问题讨论】:

    标签: python django apache import quantlib


    【解决方案1】:

    与所有 SWIG 生成的模块一样,QuantLib 不是纯 Python 模块,而是依赖于底层 C++ 共享库(应称为 libQuantLib.so)。这必须可由 Apache 加载;因此,请确保它在 PATH 中,ld 已缓存它,或者它具有所需的权限。

    【讨论】:

    • 您能否详细说明“该 ld 已缓存它”。我不确定你的意思。我可以从 python 交互式解释器和 django 开发服务器导入 QuantLib。所以,一定有一些特定于 apache 的东西。
    • 我的意思是确保您将路径添加到 /etc/ld.so.conf 并在系统需要时运行 ldconfig。您可能还需要设置 LD_LIBRARY_PATH。
    猜你喜欢
    • 2017-12-26
    • 2013-12-03
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    相关资源
    最近更新 更多