Can not import packages in python when embedding p

2019-07-23 16:57发布

问题:

I am trying to develop a python optimization script which can be called by C++ program. I start with a small example. At first it runs well; but when I try to import some package in python like "import numpy", there is runtime error (there is no compile error) such as:

 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

I search for solutions of similar questions such like Numpy import fails when embedding python in c, which suggests to use "-Xlinker -export-dynamic" when compiling to link dynamically. However, my compiler shows the following error:

ld: unknown option: -export-dynamic
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I guess it might because I am using Mac instead of linux. In the official document https://docs.python.org/2/extending/embedding.html#embedding-python-in-c, it says the OS-specific option could be obtained by either the command "*/python2.7-config --ldflags" or

import sysconfig
sysconfig.get_config_var('LINKFORSHARED')

However, the former does not give a useful option; and the latter just returns "-u _PyMac_Error" (Someones says it is a python bug). Then I don't know how to do next.

Any help? It would be highly appreciated. I am using Mac OS X 10.12.4.

标签: python c++ embed