扩展中的致命错误:PyThreadState_Get:在 Mac 上将 SWIG 与 Anaconda3 结合使用时没有当前线程
Fatal error in extension: PyThreadState_Get: no current thread when using SWIG with Anaconda3 on a Mac
我只在 anaconda3 和 Mac 上遇到这个错误。有人对如何解决这个问题有什么建议吗?
这是 test.i
文件。
# test.i
%module test
%{
int hello();
%}
这是 test.c
文件。
//test.c
#include <stdio.h>
int hello() {
printf("Hello\n");
return 0;
}
这是创建扩展的编译步骤。
$ 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 环境。
请参阅下面的最小工作示例。
问题很可能是您正尝试 link 针对 python3.6 库,而 python 可执行文件已经具有 python 库 link编入。
尝试做与 setup.py 相同的事情(这对您的 github 示例很有效):
#!/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"],)
您将看到这会创建一个 link 命令,该命令使用 -undefined dynamic_lookup
而没有明确地 linking 针对 python3.6m 或 ld 库。
python3-config --cflags
不应该 不 在为 Python 创建扩展模块时使用,也不应该总是 link 到 Python 图书馆。相反,您应该查询 Python 本身(特别是 distutils sysconfig 模块)以获取适当的值。
应该通过以下方式查询编译器标志:
python -c "from distutils import sysconfig; print(sysconfig.get_config_vars('CFLAGS'))"
应通过以下方式查询 linker 标志:
python -c "from distutils import sysconfig; print(sysconfig.get_config_var('BLDSHARED').split(' ', 1)[1])"
我只在 anaconda3 和 Mac 上遇到这个错误。有人对如何解决这个问题有什么建议吗?
这是 test.i
文件。
# test.i
%module test
%{
int hello();
%}
这是 test.c
文件。
//test.c
#include <stdio.h>
int hello() {
printf("Hello\n");
return 0;
}
这是创建扩展的编译步骤。
$ 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 环境。
请参阅下面的最小工作示例。
问题很可能是您正尝试 link 针对 python3.6 库,而 python 可执行文件已经具有 python 库 link编入。
尝试做与 setup.py 相同的事情(这对您的 github 示例很有效):
#!/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"],)
您将看到这会创建一个 link 命令,该命令使用 -undefined dynamic_lookup
而没有明确地 linking 针对 python3.6m 或 ld 库。
python3-config --cflags
不应该 不 在为 Python 创建扩展模块时使用,也不应该总是 link 到 Python 图书馆。相反,您应该查询 Python 本身(特别是 distutils sysconfig 模块)以获取适当的值。
应该通过以下方式查询编译器标志:
python -c "from distutils import sysconfig; print(sysconfig.get_config_vars('CFLAGS'))"
应通过以下方式查询 linker 标志:
python -c "from distutils import sysconfig; print(sysconfig.get_config_var('BLDSHARED').split(' ', 1)[1])"