Embedding python: Version inconsistent with ProgramFullPath

十年热恋 提交于 2019-12-11 05:56:21

问题


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.


回答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.



来源:https://stackoverflow.com/questions/39078683/embedding-python-version-inconsistent-with-programfullpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!