我正在尝试开发一个可以被C ++程序调用的python优化脚本。我从一个小例子开始。起初运行良好;但是当我尝试在python中导入一些类似于" import numpy"时,会出现运行时错误(没有编译错误),例如:
Traceback (most recent call last):
File "/*/C_nominal/addition.py", line 6, in <module>
import numpy
File "/*/anaconda/lib/python2.7/site-packages/numpy/__init__.py", line 142, in <module>
from . import add_newdocs
File "/*/anaconda/lib/python2.7/site-packages/numpy/add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "/*/anaconda/lib/python2.7/site-packages/numpy/lib/__init__.py", line 8, in <module>
from .type_check import *
File "/*/anaconda/lib/python2.7/site-packages/numpy/lib/type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "/*/anaconda/lib/python2.7/site-packages/numpy/core/__init__.py", line 58, in <module>
from numpy.testing.nosetester import _numpy_tester
File "/*/anaconda/lib/python2.7/site-packages/numpy/testing/__init__.py", line 12, in <module>
from . import decorators as dec
File "/*/anaconda/lib/python2.7/site-packages/numpy/testing/decorators.py", line 21, in <module>
from .utils import SkipTest
File "/*/anaconda/lib/python2.7/site-packages/numpy/testing/utils.py", line 15, in <module>
from tempfile import mkdtemp, mkstemp
File "/*/anaconda/lib/python2.7/tempfile.py", line 32, in <module>
import io as _io
File "/*/anaconda/lib/python2.7/io.py", line 51, in <module>
import _io
ImportError: dlopen(/*/anaconda/lib/python2.7/lib-dynload/_io.so, 2): Symbol not found: __PyCodecInfo_GetIncrementalDecoder
Referenced from: /*/anaconda/lib/python2.7/lib-dynload/_io.so
Expected in: flat namespace
in /*/anaconda/lib/python2.7/lib-dynload/_io.so
Failed to load Segmentation fault: 11
我搜索类似问题的解决方案,例如 Numpy import fails when embedding python in c ,建议使用&#34; -Xlinker -export-dynamic&#34;编译时动态链接。但是,我的编译器显示以下错误:
ld: unknown option: -export-dynamic
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我想这可能是因为我使用的是Mac而不是linux。在官方文档 https://docs.python.org/2/extending/embedding.html#embedding-python-in-c 中,它表示可以通过获取特定于操作系统的选项 命令&#34; * / python2.7-config --ldflags&#34;或
import sysconfig
sysconfig.get_config_var('LINKFORSHARED')
然而,前者没有提供有用的选择;而后者只是返回&#34; -u _PyMac_Error&#34; (有人说这是一个python bug)。然后我不知道接下来该怎么做。
有任何帮助吗?非常感谢。我使用的是Mac OS X 10.12.4。