1

I have anaconda Python first on my path, but a simple Python embedding example shows my Mac system python version instead, even though ProgramFullPath correctly points to anaconda python. Is there a way to correctly find / use anaconda python?

Minimal example:

#include <Python.h>
#include <stdio.h>
int main(void) {
 Py_Initialize();
 printf("Python version:\n%s\n", Py_GetVersion());
 printf("Python Program Full Path:\n%s\n", Py_GetProgramFullPath());
 Py_Finalize();
 return 0;
}

I compile with,

gcc `python-config --cflags` example.c `python-config --ldflags`

or, expanding the results of the python-config calls,

gcc -I/Users/ryandwyer/anaconda/include/python2.7 \
 -I/Users/ryandwyer/anaconda/include/python2.7 \
 -fno-strict-aliasing -I/Users/ryandwyer/anaconda/include \
 -arch x86_64 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes \
 example.c -lpython2.7 -ldl -framework CoreFoundation -u _PyMac_Error

Running the program gives,

Python version:
2.7.5 (default, Mar 9 2014, 22:15:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)]
Python Program Full Path:
/Users/ryandwyer/anaconda/bin/python

This seems to be the same problem as Embed python in c++: choose python version. I have also tried setting PYTHONHOME, Py_SetProgramName, Py_SetPythonHome, but cannot get Python_GetVersion() to return the anaconda version.

asked Aug 22, 2016 at 11:44

1 Answer 1

1

There was a partial answer in the post you linked.

Option 1: Run your program as follows

LD_LIBRARY_PATH=/path_to_anaconda/lib ./program

Option 2: Run the following command in the terminal, then run your program

export LD_LIBRARY_PATH=/path_to_anaconda/lib ./program

Option 3: Add the following line to the end of your .bashrc file

LD_LIBRARY_PATH=/path_to_anaconda/lib

Why do you have to do this when embedding python, but not when running the interpreter normally? I have no idea, but if some Python/C wizard stumbles on this post I'd love to know why.

answered Oct 25, 2017 at 1:08
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.