嵌入python:版本与ProgramFullPath不一致 [英] Embedding python: Version inconsistent with ProgramFullPath
问题描述
我首先使用anaconda Python,但是一个简单的Python嵌入示例显示了我的Mac系统python版本,即使ProgramFullPath
正确指向anaconda python.有没有一种方法可以正确地找到/使用蟒蛇蟒蛇?
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?
最小示例:
#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;
}
我编译,
gcc `python-config --cflags` example.c `python-config --ldflags`
或扩展python-config
调用的结果,
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
运行程序会给出
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
这似乎与在c ++中嵌入python相同:选择python版本.我也尝试设置PYTHONHOME
,Py_SetProgramName
,Py_SetPythonHome
,但无法获取Python_GetVersion()
返回anaconda版本.
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.
推荐答案
您链接的帖子中有部分答案.
There was a partial answer in the post you linked.
选项1:如下运行程序
LD_LIBRARY_PATH =/path_to_anaconda/lib ./程序
LD_LIBRARY_PATH=/path_to_anaconda/lib ./program
选项2:在终端中运行以下命令,然后运行程序
Option 2: Run the following command in the terminal, then run your program
导出LD_LIBRARY_PATH =/path_to_anaconda/lib ./程序
export LD_LIBRARY_PATH=/path_to_anaconda/lib ./program
选项3::将以下行添加到.bashrc文件的末尾
Option 3: Add the following line to the end of your .bashrc file
LD_LIBRARY_PATH =/path_to_anaconda/lib
LD_LIBRARY_PATH=/path_to_anaconda/lib
为什么在嵌入python时需要这样做,但在正常运行解释器时却不需要这样做?我不知道,但是如果有些Python/C向导在这篇文章上绊倒了,我很想知道为什么.
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.
这篇关于嵌入python:版本与ProgramFullPath不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!