问题
I only get this error with anaconda3 and swig on a Mac. Does anyone have any suggestions on how to resolve this?
This is the test.i
file.
# test.i
%module test
%{
int hello();
%}
This is the test.c
file.
//test.c
#include <stdio.h>
int hello() {
printf("Hello\n");
return 0;
}
This is the compilation steps for creating the extension.
$ swig -python test.i
$ cc -c $(python3-config --cflags) test.c test_wrap.c
$ cc -bundle -L/Users/$USER/miniconda3/lib/python3.6/config-3.6m-darwin -lpython3.6m -ldl test.o test_wrap.o -o _test.so
$ python test.py
Fatal Python error: PyThreadState_Get: no current thread
[1] 97445 abort python test.py
Again, there's no error with any other operating system. They corresponding steps work. It works with Homebrew Python2 and works with Homebrew Python3. It also works with Anaconda2. But it does not work with Anaconda3 or a Anaconda3 environment.
See below for a minimal working example.
https://github.com/kdheepak/mwe-swig-python3-anaconda
回答1:
The problem is likely that you are trying to link against the python3.6 library whereas the python executable already has the python library linked in.
Try doing the same as what a setup.py would do (this works fine with your github example):
#!/usr/bin/env python
from distutils.core import setup, Extension
test_module = Extension('_test', sources=['test_wrap.c', 'test.c'],)
setup (name = 'test', version = '0.1', ext_modules = [test_module], py_modules = ["test"],)
You will see that this creates a link command that uses -undefined dynamic_lookup
without explicitly linking against the python3.6m or ld libraries.
回答2:
python3-config --cflags
should not be used when creating an extension module for Python, nor should you always link to a Python library. Instead you should query Python itself (in particular, the distutils sysconfig module) to get the appropriate values.
The compiler flags should be queried via:
python -c "from distutils import sysconfig; print(sysconfig.get_config_vars('CFLAGS'))"
The linker flags should be queried via:
python -c "from distutils import sysconfig; print(sysconfig.get_config_var('BLDSHARED').split(' ', 1)[1])"
来源:https://stackoverflow.com/questions/48289858/fatal-error-in-extension-pythreadstate-get-no-current-thread-when-using-swig-w