Import of SWIG python module fails with apache

2019-09-07 10:40发布

问题:

Importing a python mdule throws an exception in django when I run with apache. The same source code works fine with the django development server. I can also import the module from the command line. The module is a Python SWIG library. I have researched similar problems on the web but nothing has helped (forward slashes, setting PYTHONPATH, permission check...).

I do understand that in the print statements at the end of the post there are double slashes in the file name but my understanding (I might be wrong) is that this is ok.

Here are the 3 scenario of which one fails:

From the command line I can execute the following and it works fine:

import QuantLib

Using the django development server I can run the following code in my view with no errors:

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

Now, if I move to apache with the following script I get the following error using the same view as above. The file is django_wsgi:

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

******ERROR IN BROWSER

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  

*******SOURCE CODE THAT FAILS D:\Program Files (x86)\Python27\lib\site-packages\QuantLib\QuantLib.py in 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

**********LOCAL VARIABLES IN APACHE WHEN EXCEPTION THROWN:

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)  

**********OUTPUT FROM PRINT STATEMENTS IN SOURCE CODE WHEN RUNNING WITH 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] ****************************  

**********OUTPUT FROM PRINT STATEMENTS IN SOURCE CODE WHEN DOING IMPORT FROM PYTHON INTERACTIVE INTERPRETER

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

**********OUTPUT FROM PRINT STATEMENTS IN SOURCE CODE WHEN RUNNING DJANGO DEVELOPMENT SERVER

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

回答1:

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.