Import of SWIG python module fails with apache

2024/10/14 14:14:37

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 _QuantLibreturn _QuantLibif fp is not None:try:    #TG startprint '***********TG***************'print fpprint pathnameprint descriptionprint '****************************'#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)  
****************************  
Answer

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.

https://en.xdnf.cn/q/117948.html

Related Q&A

Pro-Football-Reference Team Stats XPath

I am using the scrapy shell on this page Pittsburgh Steelers at New England Patriots - September 10th, 2015 to pull individual team stats. For example, I want to pull total yards for the away team (46…

How to delete the last item of a collection in mongodb

I made a program with python and mongodb to do some diaries. Like thisSometimes I want to delete the last sentence, just by typing "delete!" But I dont know how to delete in a samrt way. I do…

Python+kivy+SQLite: How to set label initial value and how to update label text?

everyone,I want to use kivy+Python to display items from a db file. To this purpose I have asked a question before: Python+kivy+SQLite: How to use them together The App in the link contains one screen.…

how to debug ModelMultipleChoiceField [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 7 years ago.Improve…

Standardization/preprocessing for 4-dimensional array

Id like to standardize my data to zero mean and std = 1. The shape of my data is 28783x4x24x7, and it can thought of as 28783 images with 4 channels and dimensions 24x7. The channels need to be standar…

My Python number guessing game

I have been trying to make a number guessing game for Python and so far it has altogether gone quite well. But what keeps bugging me is that it resets the number on every guess so that it is different,…

Class that takes another class as argument, copies behavior

Id like to create a class in Python that takes a single argument in the constructor, another Python class. The instance of the Copy class should have all the attributes and methods of the original clas…

Simple python script to get a libreoffice base field and play on vlc

Ive banged my head for hours on this one, and I dont understand the LibreOffice macro api well enough to know how to make this work:1) This script works in python:#!/usr/bin/env python3 import subproce…

Print month using the month and day

I need to print month using the month and day. But I cannot seem to move the numbers after 1 to the next line using Python.# This program shows example of "November" as month and "Sunday…

Maya: Defer a script until after VRay is registered?

Im trying to delay a part of my pipeline tool (which runs during the startup of Maya) to run after VRay has been registered. Im currently delaying the initialization of the tool in a userSetup.py like…