如何通过Boost.Python从python文件导入函数 [英] How to import a function from python file by Boost.Python

查看:147
本文介绍了如何通过Boost.Python从python文件导入函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

boost.python是我的新手. 我回顾了很多建议使用boost.python来与python一起使用,但是仍然不容易理解并为我找到解决方案.

I am totally new to boost.python. I reviewed a lot of recommending of using boost.python to apply with python, however still not easy to understand and find a solution for me.

我想要的是直接从python"SourceFile"导入函数或类

What I want is to import a function or class that directly from a python "SourceFile"

示例文件: Main.cpp MyPythonClass.py

Example File: Main.cpp MyPythonClass.py

让我们说如果"MyPythonClass.py"中有一个带有"bark()"函数的"Dog"类,如何在cpp中获取回调并发送参数?

Let's says if there is a "Dog" class in "MyPythonClass.py" with "bark()" function, how do I get callback and send argument in cpp?

我不知道该怎么办! 请帮帮我!

I have no idea what I should do! Please help me!

推荐答案

当需要从C ++调用Python且C ++拥有主要功能时,则必须嵌入 C ++中的Python中断器程序. Boost.Python API并不是Python/C API的完整包装,因此人们可能会发现需要直接调用Python/C API的某些部分.不过,Boost.Python的API可以使互操作性变得更容易.考虑阅读官方的Boost.Python 嵌入教程以获取更多信息.

When one needs to call Python from C++, and C++ owns the main function, then one must embed the Python interrupter within the C++ program. The Boost.Python API is not a complete wrapper around the Python/C API, so one may find the need to directly invoke parts of the Python/C API. Nevertheless, Boost.Python's API can make interoperability easier. Consider reading the official Boost.Python embedding tutorial for more information.

这是嵌入Python的C ++程序的基本框架:

Here is a basic skeleton for a C++ program that embeds Python:

int main()
{
  // Initialize Python.
  Py_Initialize();

  namespace python = boost::python;
  try
  {
    ... Boost.Python calls ...
  }
  catch (const python::error_already_set&)
  {
    PyErr_Print();
    return 1;
  }

  // Do not call Py_Finalize() with Boost.Python.
}

嵌入Python时,可能有必要增加模块搜索路径通过 PYTHONPATH 可以从自定义位置导入模块.

When embedding Python, it may be necessary to augment the module search path via PYTHONPATH so that modules can be imported from custom locations.

// Allow Python to load modules from the current directory.
setenv("PYTHONPATH", ".", 1);
// Initialize Python.
Py_Initialize();

通常,Boost.Python API提供了一种以类似Python的方式编写C ++代码的方法.以下示例演示在C ++中嵌入Python解释器,并让C ++导入MyPythonClass从磁盘上的Python模块,实例化MyPythonClass.Dog的实例,然后在Dog实例上调用bark():

Often times, the Boost.Python API provides a way to write C++ code in a Python-ish manner. The following example demonstrates embedding a Python interpreter in C++, and having C++ import a MyPythonClass Python module from disk, instantiate an instance of MyPythonClass.Dog, and then invoking bark() on the Dog instance:

#include <boost/python.hpp>
#include <cstdlib> // setenv

int main()
{
  // Allow Python to load modules from the current directory.
  setenv("PYTHONPATH", ".", 1);
  // Initialize Python.
  Py_Initialize();

  namespace python = boost::python;
  try
  {
    // >>> import MyPythonClass
    python::object my_python_class_module = python::import("MyPythonClass");

    // >>> dog = MyPythonClass.Dog()
    python::object dog = my_python_class_module.attr("Dog")();

    // >>> dog.bark("woof");
    dog.attr("bark")("woof");
  }
  catch (const python::error_already_set&)
  {
    PyErr_Print();
    return 1;
  }

  // Do not call Py_Finalize() with Boost.Python.
}

给出一个包含以下内容的MyPythonClass模块:

Given a MyPythonClass module that contains:

class Dog():
    def bark(self, message):
        print "The dog barks: {}".format(message)

以上程序输出:

The dog barks: woof

这篇关于如何通过Boost.Python从python文件导入函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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