我一直在寻找我的问题的答案很长一段时间,但我找到的那些似乎都没有解决我的问题。
我正在尝试使用Python提供的功能(Python.h,Py_xxx函数等)将Python嵌入到我的C ++代码中。但是,我在让我的C ++程序调用正确的Python解释器方面遇到了麻烦。实际上,我的机器上有几个解释器(顺便说一下,它是运行OSX 10.7.5的Mac)。我有预装OS的默认版本的Python(版本2.7.1),我有另一个由anaconda安装的版本(版本2.7.7)。我需要使用anaconda安装的版本,因为我需要额外的anaconda库,默认情况下没有安装OSX的Python。
我的C ++代码如下:
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");
结果:
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)]
然而,当我在终端中运行anaconda的python时,这就是我得到的
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
默认的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.
所以看来,即使我为可执行文件指定了另一条路径,也会调用OSX的默认python(并且与我之后尝试导入的anaconda的库冲突)。
我的问题很简单:我做错了什么,为什么我通过Py_Setxxx指定的路径没有指向正确的可执行文件?
非常感谢你的帮助!
亚历
答案 0 :(得分:2)
我遇到了同样的问题。我的解决方案是调用这样的程序:
DYLD_LIBRARY_PATH=/path_to_anaconda/lib ./program
这是因为运行时使用的共享库是从错误的原始OSX目录加载的。 (Linux中的LD_LIBRARY_PATH)
答案 1 :(得分:0)
使用“import os”和“print(os.sys.path)”将python置于主页,然后:
Py_SetPythonHome((wchar_t*)L"/home/c/anaconda3/lib/python35.zip:"
"/home/c/anaconda3/lib/python3.5:"
"/home/c/anaconda3/lib/python3.5/plat-linux:"
"/home/c/anaconda3/lib/python3.5/lib-dynload:"
"/home/c/anaconda3/lib/python3.5/site-packages:"
"/home/c/anaconda3/lib/python3.5/site-packages/setuptools-27.2.0-py3.5.egg:"
"/home/c/anaconda3/lib/python3.5/site-packages/Sphinx-1.4.6-py3.5.egg");
答案 2 :(得分:-1)
Tomas提供的答案对我有所帮助。我在类似的帖子中添加了一些其他选项:
https://stackoverflow.com/a/46922332/8828614
您关联的帖子中有部分答案。
选项1:按如下方式运行程序
LD_LIBRARY_PATH = / path_to_anaconda / lib ./program
选项2:在终端中运行以下命令,然后运行您的 程序
export LD_LIBRARY_PATH = / path_to_anaconda / lib ./program
选项3:将以下行添加到.bashrc文件的末尾
LD_LIBRARY_PATH = / path_to_anaconda / lib中
为什么在嵌入python时必须这样做,而不是在运行时 翻译通常吗?我不知道,但如果有一些Python / C向导 在这篇文章中偶然发现我很想知道原因。