【问题标题】:Embedding python: Version inconsistent with ProgramFullPath嵌入python:版本与ProgramFullPath不一致
【发布时间】:2016-08-22 11:44:48
【问题描述】:

我的路径上首先有 anaconda Python,但一个简单的 Python 嵌入示例显示了我的 Mac 系统 python 版本,即使 ProgramFullPath 正确指向 anaconda python。有没有办法正确查找/使用 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

这似乎和Embed python in c++: choose python version 是同一个问题。我也试过设置PYTHONHOMEPy_SetProgramNamePy_SetPythonHome,但无法让Python_GetVersion()返回anaconda版本。

【问题讨论】:

    标签: python python-2.7 anaconda embedding python-embedding


    【解决方案1】:

    您链接的帖子中有部分答案。

    选项 1:按如下方式运行您的程序

    LD_LIBRARY_PATH=/path_to_anaconda/lib ./program

    选项2:在终端运行以下命令,然后运行你的程序

    导出 LD_LIBRARY_PATH=/path_to_anaconda/lib ./程序

    选项 3:将以下行添加到 .bashrc 文件的末尾

    LD_LIBRARY_PATH=/path_to_anaconda/lib

    为什么嵌入python的时候一定要这样做,而正常运行解释器的时候就不需要了?我不知道,但如果某个 Python/C 向导偶然发现了这篇文章,我很想知道原因。

    【讨论】:

      猜你喜欢
      • 2021-10-17
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2014-10-23
      相关资源
      最近更新 更多