在C ++中嵌入python:选择python版本 [英] Embed python in c++: choose python version

查看:202
本文介绍了在C ++中嵌入python:选择python版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找问题的答案,但是我发现没有一个问题能解决我的问题.

I've been searching an answer to my question for quite a while but none of the ones that I have found seems to solve my problem.

我正在尝试使用Python提供的功能(Python.h,Py_xxx函数等)将Python嵌入我的C ++代码中.但是,在使C ++程序调用正确的Python解释器时遇到了麻烦.确实,我的机器上存在几个解释器(顺便说一下,这是一台运行OSX 10.7.5的Mac).我已经预安装了操作系统的默认版本的Python(版本2.7.1),并且我已经由anaconda安装了另一个版本(版本2.7.7).我需要使用anaconda安装的版本,因为我需要anaconda可用的额外库,而这些库在默认情况下并未与OSX的Python一起安装.

I'm trying to embed Python within my C++ code with the functionalities provided by Python (Python.h, Py_xxx functions, etc.). However, I'm having troubles in getting my C++ program to call the right Python interpreter. Indeed, there exist several interpreters on my machine (which by the way is a Mac running OSX 10.7.5). I have the default version of Python preinstalled with the OS (ver 2.7.1) and I have another version installed by anaconda (ver 2.7.7). I need to use the version installed by anaconda because I need extra libraries available with anaconda that are not installed by default with OSX's Python.

我的C ++代码如下:

My C++ code is as follows:

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时,这就是我得到的

However, when I run anaconda's python in the terminal, here is what I get

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的结果

And the result with default's 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的库冲突).

So it seems that, even if I'm specifying another path for the executable, OSX's default python is called (and conflicts with anaconda's libraries that I'm trying to import later).

我的问题很简单:我在做什么错?为什么我通过Py_Setxxx指定的路径没有指向正确的可执行文件?

My question is thus simple: what am I doing wrong and why do the paths that I specify through Py_Setxxx do not point to the right executable ?

非常感谢您的帮助!

亚历克斯

推荐答案

我遇到了同样的问题.对我来说,解决方案是这样调用程序:

I've experienced the same problem. The solution for me was to call program like this:

DYLD_LIBRARY_PATH=/path_to_anaconda/lib ./program

这是因为在运行时使用的共享库是从错误的原始OSX目录加载的. (在Linux中为LD_LIBRARY_PATH)

It's because the shared libraries used at runtime were loaded from wrong, original OSX directory. (LD_LIBRARY_PATH in linux)

这篇关于在C ++中嵌入python:选择python版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆