Fatal Python error when using SWIG under OSX - python

I have troubles using a Python interface generated with SWIG (I have OSX 10.11.12). After compiling and linking everything together as such:
swig -python erk_integrator.i
gcc -c -fPIC -O3 model.c auxiliary_functions.c timing_functions.c
gcc -c -fPIC -O3 erk_integrator.c erk_integrator_wrap.c -I. -I/usr/local/include/python2.7
gcc -lpython -dynamiclib model.o erk_integrator.o erk_integrator_wrap.o auxiliary_functions.o timing_functions.o -o _erk_integrator.so
I try a test script, but Python throws a fatal error:
/usr/local/bin/python test_erk.py
Fatal Python error: PyThreadState_Get: no current thread
Abort trap: 6
But when I run
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 test_erk.py
everything works as it should. However, I need to use /usr/local/bin/python (from Homebrew) instead of the system Python.
I think something goes wrong in the linking step. Many thanks for helping!

This blog post helped me solve it: blog.tim-smith.us/2015/09/python-extension-modules-os-x
It turns out on OSX you need
-undefined dynamic_lookup
instead of
-lpython

Related

Installing Python 2.4 on CentOS 8 fails with segmentation fault

I am having problems installing Python 2.4.6 on CentOS 8.
I need the old Python 2.4.6 because I have some apps running which require Python 2.4.
I downloaded the Python 2.4.6.tgz package, extracted it and run "./configure" which works.
When I try to run "make", I see a lot of warnings and at the end the following error message is shown:
gcc -pthread -Xlinker -export-dynamic -o python \
Modules/python.o \
libpython2.4.a -lpthread -ldl -lutil -lm
libpython2.4.a(posixmodule.o): In function `posix_tmpnam':
/usr/local/src/Python-2.4.6/./Modules/posixmodule.c:6240: warning: the use of `tmpnam_r' is dangerous, better use `mkstemp'
libpython2.4.a(posixmodule.o): In function `posix_tempnam':
/usr/local/src/Python-2.4.6/./Modules/posixmodule.c:6195: warning: the use of `tempnam' is dangerous, better use `mkstemp'
case $MAKEFLAGS in \
*-s*) CC='gcc -pthread' LDSHARED='gcc -pthread -shared' OPT='-DNDEBUG -g -O3 -Wall -Wstrict-prototypes' ./python -E ./setup.py -q build;; \
*) CC='gcc -pthread' LDSHARED='gcc -pthread -shared' OPT='-DNDEBUG -g -O3 -Wall -Wstrict-prototypes' ./python -E ./setup.py build;; \
esac
/bin/sh: line 1: 5296 Segmentation fault (core dumped) CC='gcc -pthread' LDSHARED='gcc -pthread -shared' OPT='-DNDEBUG -g -O3 -Wall -Wstrict-prototypes' ./python -E ./setup.py build
make: *** [Makefile:342: sharedmods] Error 139
Any idea, what might be wrong here? If required, I can post the whole output after I run "make" (which is quite long).
Thanks a lot in advance.
I would recommend to go a different route than installing Python 2.4 on your OS. That thing is ancient, it can only bring security issues.
So if your apps don't work with even Python 2.7, perhaps you have better luck with running Python 2.4 in a Docker. See e.g. https://github.com/pantuza/docker-python-2.4.3 for a docker image with Python 2.4 and https://www.docker.com/get-started for an introduction on docker.
And probably the next step should be porting those apps to Python 3, such that you can get rid of that ancient Python version for good.

Can't install C-based modules like lxml and Cython on Python 3.6 on Raspberry Pi

I built Python 3.6 from source and am trying to install lxml. Trying to install it from pip gives the following error:
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/include/libxml2 -Isrc -Isrc/lxml/includes -I/usr/local/include/python3.6m -c src/lxml/etree.c -o build/temp.linux-armv6l-3.6/src/lxml/etree.o -w
src/lxml/etree.c:91:20: fatal error: Python.h: No such file or directory
compilation terminated.
Compile failed: command 'gcc' failed with exit status 1
cc -I/usr/include/libxml2 -I/usr/include/libxml2 -c /tmp/xmlXPathInitln68cjsn.c -o tmp/xmlXPathInitln68cjsn.o
cc tmp/xmlXPathInitln68cjsn.o -L/usr/lib -lxml2 -o a.out
error: command 'gcc' failed with exit status 1
The same error is raised if I try to install Cython to compile lxml from source. I installed libxml2-dev and libxslt1-dev, but python3.6-dev, which is apparently also required, is not found in Raspbian repository. I added the following line to /etc/apt/sources.list:
deb http://ftp.de.debian.org/debian sid main
But when trying to install it afterwards, I receive the following error:
The following packages have unmet dependencies:
libdbus-1-3 : Breaks: dbus (< 1.9.16-1~) but 1.6.8-1+deb7u1 is to be installed
E: Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages.
In fact, this error is raised on any package that is found in Debian repository that is not in Raspbian one. What can I do about this?
Since you installed Python from source, you should have the header files already. Should also use header files for the version that you built as opposed to the version in the package manager.
Either
Add the source code directory to includes used by GCC (gcc -I<dir>) or
Install the from source package in a system directory that is automatically included like /usr/include or /usr/local/include
The second option is probably easiest and should be possible from the standard configure/make scripts.

Calling cv::cuda::HOG from python

I need to access the cuda modules of opencv in C++ from python. I tried wrapping the c++ functions using Boost however I get the following error.
ImportError: ../build/lib.linux-x86_64-2.7/pyBoostHOG.so: undefined
symbol: _ZN2cv4cuda6Stream4NullEv.
Is there any way by which I can access the opencv cuda modules from python?
Thanks in advance..
I was facing the same error. And this is how I was compiling the .so file:
g++ `pkg-config opencv4 --cflags --libs` -fPIC -c flow.cpp
g++ -Wall -O3 -shared -o flow.so flow.o `pkg-config opencv4 --cflags --libs`
The error was resolved when I changed my make file to the following:
g++ `pkg-config opencv4 --cflags --libs` -fPIC -c flow.cpp
g++ -Wall -O3 -shared flow.o -o flow.so `pkg-config opencv4 --cflags --libs`
I had the same error.
I figured out that I must include opencv2/core/cuda.hpp file (symbol _ZN2cv4cuda6Stream4NullEv reffers to: cv::cuda::Stream::Null()
But this doesn't solve the problem, because when I tried to make wrap thought swig and include these file, new error:
.../include/opencv2/core/cuda.hpp:317: Error: Syntax error in input(1).
is displayed.
Compiling another program using cv::cuda::Stream::Null() cause not error.

Cython: Compiling and Cannot Find Library Mac OSX 10.12

I am just getting started with Cython and am trying to compile a "Hello World" script. I am trying to use gcc -Os /User/Documents/Python/Test\ Python/helloCopy.c -I/Library/Frameworks/Python.framework/Versions/3.5/include/python3.5m -l, but I don't know what to add after the -l. Other forum pages say to "include -lpython2.7 (or whatever version of Python you're using) on the linker command-line" but that produces ld: library not found for -lpython3.5
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Should I be directing the -l to a particular folder?
I do not know what resource you're using, but this does not say anything about a -l flag. It suggests
cython -a helloCopy.pyx
This creates a yourmod.c file, and the -a switch produces an annotated html file of the source code. Pass the -h flag for a complete list of supported flags.
gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python2.7 -o helloCopy.so helloCopy.c
(Linux)
On macOS I would try to compile with
gcc -I/usr/bin/python -o helloCopy.so helloCopy.c
to use the standard version of Python.

Clang linker issues when installing Distance python library

I'm trying to install Distance for python on my mac (OS X Yosemite).
After downloading the package and unpacking it, I run (as described on their page):
python setup.py install --with-c
From this I get the following error message:
running build_ext
building 'distance.cdistance' extension
gcc -fno-strict-aliasing -I/Users/me/anaconda/envs/name/include - arch x86_64 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/me/anaconda/envs/name/include/python2.7 -c cdistance/distance.c -o build/temp.macosx-10.5-x86_64-2.7/cdistance/distance.o
gcc -bundle -undefined dynamic_lookup -L/Users/me/anaconda/envs/name/lib -arch x86_64 -arch x86_64 build/temp.macosx-10.5-x86_64-2.7/cdistance/distance.o -L/Users/me/anaconda/envs/name/lib -o build/lib.macosx-10.5-x86_64-2.7/distance/cdistance.so
ld: library not found for -lgcc_s.10.5
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'gcc' failed with exit status 1
From what I understood from several posts like this one or this previously asked question, it looks like clang, which is called through the command gcc, can't find the libgcc library.
I ran find /usr/ -name libgcc*and this if what I get:
/usr//lib/libgcc_s.1.dylib
/usr//lib/libgcc_s.10.4.tbd
/usr//lib/libgcc_s.10.5.tbd
/usr//local/Cellar/gcc/5.2.0/lib/gcc/5/gcc/x86_64-apple-darwin14.4.0/5.2.0/i386/libgcc.a
/usr//local/Cellar/gcc/5.2.0/lib/gcc/5/gcc/x86_64-apple-darwin14.4.0/5.2.0/i386/libgcc_eh.a
/usr//local/Cellar/gcc/5.2.0/lib/gcc/5/gcc/x86_64-apple-darwin14.4.0/5.2.0/libgcc.a
/usr//local/Cellar/gcc/5.2.0/lib/gcc/5/gcc/x86_64-apple-darwin14.4.0/5.2.0/libgcc_eh.a
/usr//local/Cellar/gcc/5.2.0/lib/gcc/5/libgcc_ext.10.4.dylib
/usr//local/Cellar/gcc/5.2.0/lib/gcc/5/libgcc_ext.10.5.dylib
/usr//local/Cellar/gcc/5.2.0/lib/gcc/5/libgcc_s.1.dylib
/usr//local/Cellar/gcc/5.2.0/lib/gcc/5/libgcc_s_ppc64.1.dylib
/usr//local/Cellar/gcc/5.2.0/lib/gcc/5/libgcc_s_x86_64.1.dylib
/usr//local/gfortran/lib/gcc/x86_64-apple-darwin14/5.1.0/i386/libgcc.a
/usr//local/gfortran/lib/gcc/x86_64-apple-darwin14/5.1.0/i386/libgcc_eh.a
/usr//local/gfortran/lib/gcc/x86_64-apple-darwin14/5.1.0/libgcc.a
/usr//local/gfortran/lib/gcc/x86_64-apple-darwin14/5.1.0/libgcc_eh.a
/usr//local/gfortran/lib/libgcc_ext.10.4.dylib
/usr//local/gfortran/lib/libgcc_ext.10.5.dylib
/usr//local/gfortran/lib/libgcc_s.1.dylib
/usr//local/gfortran/lib/libgcc_s_ppc64.1.dylib
/usr//local/gfortran/lib/libgcc_s_x86_64.1.dylib
And now I'm stuck because I don't know what to do next. Basically the question is: how do I make clang to know where the library is ?
I solved the issue thanks to this link which showed that this is a very recent issue with Xcode6.
You just need to upgrade Xcode to Xcode7 and set it up as your default toolkit chain. Easy.

Categories

Resources