1

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.

Community
  • 1
  • 1
Aaron Ji
  • 11
  • 2
  • I have ever constructed a program with python and c++. It's difficulty, I confronted many problems and solved all. But I didn't face the problem liked yours. According to my experiences, make your c++ code clean, didn't reference to any python lib code. Check environment variables named PYTHONHOME PYTHONLIB PYTHONPATH. That's all, I'll go on checking. – Don Zhang Apr 17 '17 at 06:53
  • My linking code:g++ -DNDEBUG -g -fwrapv -O3 -Wall -I$PYTHONHOME -L$PYTHONLIB -L/usr/lib -lpthread -ldl  -lutil -lm  -Xlinker -export-dynamic python.cpp -o pyx -lpython3.4m – Don Zhang Apr 17 '17 at 07:21
  • Thanks a lot for your help! Unfortunately the argument '-Xlinker -export-dynamic ' does not work on my Mac OS X. Are you using linux? – Aaron Ji Apr 18 '17 at 06:55

0 Answers0