<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <!-- Copyright David Abrahams 2006. Distributed under the Boost --> <!-- Software License, Version 1.0. (See accompanying --> <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) --> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link rel="stylesheet" type="text/css" href="../boost.css"> <title>Boost.Python - <boost/python/import.hpp></title> </head> <body> <table border="0" cellpadding="7" cellspacing="0" width="100%" summary= "header"> <tr> <td valign="top" width="300"> <h3><a href="../../../../index.htm"><img height="86" width="277" alt="C++ Boost" src="../../../../boost.png" border="0"></a></h3> </td> <td valign="top"> <h1 align="center"><a href="../index.html">Boost.Python</a></h1> <h2 align="center">Header <boost/python/import.hpp></h2> </td> </tr> </table> <hr> <h2>Contents</h2> <dl class="page-index"> <dt><a href="#introduction">Introduction</a></dt> <dt><a href="#functions">Functions</a></dt> <dd> <dl class="page-index"> <dt><a href="#import-spec"><code>import</code></a></dt> </dl> </dd> <dt><a href="#examples">Examples</a></dt> </dl> <hr> <h2><a name="introduction"></a>Introduction</h2> <p>Exposes a mechanism for importing python modules.</p> <h2><a name="functions"></a>Functions</h2> <h3><a name="import-spec"></a><code>import</code></h3> <pre> object import(str name); </pre> <dl class="function-semantics"> <dt><b>Effects:</b> Imports the module named by <code>name</code>.</dt> <dt><b>Returns:</b> An instance of <a href="object.html#object-spec">object</a> which holds a reference to the imported module.</dt> </dl> <h2><a name="examples"></a>Examples</h2> <para>The following example demonstrates the use of <function>import</function> to access a function in python, and later call it from within C++.</para> <pre> #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; } </pre> <p>Revised 01 November, 2005</p> <p><i>© Copyright Stefan Seefeld 2005.</i></p> </body> </html>