Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

boost/python/import.hpp

Introduction
Function import
Examples

Exposes a mechanism for importing python modules.

object import(str name);

Effects

Imports the module named by name.

Returns

An instance of object which holds a reference to the imported module.

The following example demonstrates the use of import to access a function in python, and later call it from within C++.

#include <iostream>
#include <string>

using namespace boost::python;

void print_python_version()
{
  // Load the sys module.
  object sys = import("sys");

  // Extract the python version.
  std::string version = extract<std::string>(sys.attr("version"));
  std::cout << version << std::endl;
}

PrevUpHomeNext