扩展程序中的致命错误:PyThreadState_Get:在Mac上将SWIG与Anaconda3一起使用时,没有当前线程 [英] Fatal error in extension: PyThreadState_Get: no current thread when using SWIG with Anaconda3 on a Mac
问题描述
我只在Mac上使用anaconda3和swig出现此错误.有人对如何解决此问题有任何建议吗?
I only get this error with anaconda3 and swig on a Mac. Does anyone have any suggestions on how to resolve this?
这是test.i
文件.
# test.i
%module test
%{
int hello();
%}
这是test.c
文件.
//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
同样,任何其他操作系统都没有错误.他们相应的步骤起作用.它适用于Homebrew Python2和Homebrew Python3.它也可以与Anaconda2一起使用.但是它不适用于Anaconda3或Anaconda3环境.
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
推荐答案
问题可能是您试图链接到python3.6库,而python可执行文件已经链接了python库.
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.
尝试执行与setup.py相同的操作(这在您的github示例中很好用):
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"],)
您将看到,这将创建一个使用-undefined dynamic_lookup
的链接命令,而无需显式链接到python3.6m或ld库.
You will see that this creates a link command that uses -undefined dynamic_lookup
without explicitly linking against the python3.6m or ld libraries.
这篇关于扩展程序中的致命错误:PyThreadState_Get:在Mac上将SWIG与Anaconda3一起使用时,没有当前线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!