I've been searching an answer to my question for quite a while but none of the ones that I have found seems to solve my problem.
I'm trying to embed Python within my C++ code with the functionalities provided by Python (Python.h, Py_xxx functions, etc.). However, I'm having troubles in getting my C++ program to call the right Python interpreter. Indeed, there exist several interpreters on my machine (which by the way is a Mac running OSX 10.7.5). I have the default version of Python preinstalled with the OS (ver 2.7.1) and I have another version installed by anaconda (ver 2.7.7). I need to use the version installed by anaconda because I need extra libraries available with anaconda that are not installed by default with OSX's Python.
My C++ code is as follows:
char* python_home_ = (char*) "/anaconda";
char* program_name_ = (char*) "/anaconda/bin/python2.7";
Py_SetPythonHome(python_home_);
Py_SetProgramName(program_name_);
Py_Initialize();
printf("python home: %s\n", Py_GetPythonHome());
printf("program name: %s\n", Py_GetProgramName());
printf("get path: %s\n", Py_GetPath());
printf("get prefix: %s\n", Py_GetPrefix());
printf("get exec prefix: %s\n", Py_GetExecPrefix());
printf("get prog full path: %s\n", Py_GetProgramFullPath());
PyRun_SimpleString("import sys");
printf("path: ");
PyRun_SimpleString("print sys.path");
printf("version: ");
PyRun_SimpleString("print sys.version");
And the result:
python home: /anaconda
program name: /anaconda/bin/python2.7
get path: /anaconda/lib/python27.zip:/anaconda/lib/python2.7/:/anaconda/lib/python2.7/plat-darwin:/anaconda/lib/python2.7/plat-mac:/anaconda/lib/python2.7/plat-mac/lib-scriptpackages:/anaconda/lib/python2.7/../../Extras/lib/python:/anaconda/lib/python2.7/lib-tk:/anaconda/lib/python2.7/lib-old:/anaconda/lib/python2.7/lib-dynload
get prefix: /anaconda
get exec prefix: /anaconda
get prog full path: /anaconda/bin/python2.7
path: ['/anaconda/lib/python2.7/site-packages/sphinxcontrib_googleanalytics-0.1dev_20140616-py2.7.egg', '/anaconda/lib/python27.zip', '/anaconda/lib/python2.7', '/anaconda/lib/python2.7/plat-darwin', '/anaconda/lib/python2.7/plat-mac', '/anaconda/lib/python2.7/plat-mac/lib-scriptpackages', '/anaconda/Extras/lib/python', '/anaconda/lib/python2.7/lib-tk', '/anaconda/lib/python2.7/lib-old', '/anaconda/lib/python2.7/lib-dynload', '/anaconda/lib/python2.7/site-packages', '/anaconda/lib/python2.7/site-packages/PIL', '/anaconda/lib/python2.7/site-packages/setuptools-2.2-py2.7.egg']
version: 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)]
However, when I run anaconda's python in the terminal, here is what I get
Alexs-MacBook-Pro:lib alex$ /anaconda/bin/python2.7
Python 2.7.7 |Anaconda 1.9.1 (x86_64)| (default, Jun 2 2014, 12:48:16)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
And the result with default's python
Alexs-MacBook-Pro:lib alex$ /usr/bin/python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
So it seems that, even if I'm specifying another path for the executable, OSX's default python is called (and conflicts with anaconda's libraries that I'm trying to import later).
My question is thus simple: what am I doing wrong and why do the paths that I specify through Py_Setxxx do not point to the right executable ?
Thank you very much for your help!
Alex
I've experienced the same problem. The solution for me was to call program like this:
DYLD_LIBRARY_PATH=/path_to_anaconda/lib ./program
It's because the shared libraries used at runtime were loaded from wrong, original OSX directory. (LD_LIBRARY_PATH in linux)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With