Skip to content Skip to sidebar Skip to footer

Occasional Ctypes Error Importing Numpy From Mod_wsgi Django App

Here's the setup: Django (1.2) app on mod_wsgi that imports ctypes Python 2.6.5 Apache 2.2.3 SELinux disabled RedHat EL 5 64bit some of the file system is mounted over nfs Occasi

Solution 1:

I've run into this bug too. In my case it occurs when I exec a Python script from within a PHP script running under Apache on a 64-bit Linux system. [The Python code being run is the front-end to a pypy sandbox.] The same bit of code works fine on a 32-bit system and even works fine when the PHP script is executed directly from the command line. My "fix" has been simply to comment out that line "CFUNCTYPE(c_int)(lambda: None)" in ctypes/init.py. It's the last line of the file and is preceded by the following comment, showing that the programmer doesn't understand what's going on, either!

# XXX for whatever reasons, creating the first instance of a callback
# function is needed for the unittests on Win64 to succeed.  This MAY
# be a compiler bug, since the problem occurs only when _ctypes is
# compiled with the MS SDK compiler.  Or an uninitialized variable?
CFUNCTYPE(c_int)(lambda: None)

Clearly there's a deeper problem somewhere in cpython, but the fix works for me.


Solution 2:

In case others happen to run into this issue, my fix was to upgrade from Python 3.7 to 3.8.

This offending line: https://github.com/python/cpython/blob/3.7/Lib/ctypes/__init__.py#L273

Was removed in 3.8: https://github.com/python/cpython/blob/3.8/Lib/ctypes/__init__.py#L261-L270

Hopefully, it can save someone the headache of trying to monkey patch ctype/__init__.py which was unsuccessful for me due to the way conda pack handles standard Python libraries.

Hope that helps 🤞


Solution 3:

Consider turning SELinux off. It should solve the problem.


Post a Comment for "Occasional Ctypes Error Importing Numpy From Mod_wsgi Django App"