1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>boost/python/import.hpp</title> 5<link rel="stylesheet" href="../../boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../index.html" title="Boost.Python Reference Manual"> 8<link rel="up" href="../embedding.html" title="Chapter 6. Embedding"> 9<link rel="prev" href="../embedding.html" title="Chapter 6. Embedding"> 10<link rel="next" href="../utility_and_infrastructure.html" title="Chapter 7. Utility and Infrastructure"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table> 14<hr> 15<div class="spirit-nav"> 16<a accesskey="p" href="../embedding.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../embedding.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../utility_and_infrastructure.html"><img src="../../images/next.png" alt="Next"></a> 17</div> 18<div class="section"> 19<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 20<a name="embedding.boost_python_import_hpp"></a><a class="link" href="boost_python_import_hpp.html" title="boost/python/import.hpp">boost/python/import.hpp</a> 21</h2></div></div></div> 22<div class="toc"><dl class="toc"> 23<dt><span class="section"><a href="boost_python_import_hpp.html#embedding.boost_python_import_hpp.introduction">Introduction</a></span></dt> 24<dt><span class="section"><a href="boost_python_import_hpp.html#embedding.boost_python_import_hpp.function_import">Function 25 <code class="computeroutput"><span class="identifier">import</span></code></a></span></dt> 26<dt><span class="section"><a href="boost_python_import_hpp.html#embedding.boost_python_import_hpp.examples">Examples</a></span></dt> 27</dl></div> 28<div class="section"> 29<div class="titlepage"><div><div><h3 class="title"> 30<a name="embedding.boost_python_import_hpp.introduction"></a><a class="link" href="boost_python_import_hpp.html#embedding.boost_python_import_hpp.introduction" title="Introduction">Introduction</a> 31</h3></div></div></div> 32<p> 33 Exposes a mechanism for importing python modules. 34 </p> 35</div> 36<div class="section"> 37<div class="titlepage"><div><div><h3 class="title"> 38<a name="embedding.boost_python_import_hpp.function_import"></a><a class="link" href="boost_python_import_hpp.html#embedding.boost_python_import_hpp.function_import" title="Function import">Function 39 <code class="computeroutput"><span class="identifier">import</span></code></a> 40</h3></div></div></div> 41<pre class="programlisting"><span class="identifier">object</span> <span class="identifier">import</span><span class="special">(</span><span class="identifier">str</span> <span class="identifier">name</span><span class="special">);</span></pre> 42<div class="variablelist"> 43<p class="title"><b></b></p> 44<dl class="variablelist"> 45<dt><span class="term">Effects</span></dt> 46<dd><p> 47 Imports the module named by name. 48 </p></dd> 49<dt><span class="term">Returns</span></dt> 50<dd><p> 51 An instance of object which holds a reference to the imported module. 52 </p></dd> 53</dl> 54</div> 55</div> 56<div class="section"> 57<div class="titlepage"><div><div><h3 class="title"> 58<a name="embedding.boost_python_import_hpp.examples"></a><a class="link" href="boost_python_import_hpp.html#embedding.boost_python_import_hpp.examples" title="Examples">Examples</a> 59</h3></div></div></div> 60<p> 61 The following example demonstrates the use of import to access a function 62 in python, and later call it from within C++. 63 </p> 64<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 65<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">string</span><span class="special">></span> 66 67<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span> 68 69<span class="keyword">void</span> <span class="identifier">print_python_version</span><span class="special">()</span> 70<span class="special">{</span> 71 <span class="comment">// Load the sys module.</span> 72 <span class="identifier">object</span> <span class="identifier">sys</span> <span class="special">=</span> <span class="identifier">import</span><span class="special">(</span><span class="string">"sys"</span><span class="special">);</span> 73 74 <span class="comment">// Extract the python version.</span> 75 <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">version</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">>(</span><span class="identifier">sys</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"version"</span><span class="special">));</span> 76 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">version</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 77<span class="special">}</span> 78</pre> 79</div> 80</div> 81<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 82<td align="left"></td> 83<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p> 84 Distributed under the Boost Software License, Version 1.0. (See accompanying 85 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a> 86 </p> 87</div></td> 88</tr></table> 89<hr> 90<div class="spirit-nav"> 91<a accesskey="p" href="../embedding.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../embedding.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../utility_and_infrastructure.html"><img src="../../images/next.png" alt="Next"></a> 92</div> 93</body> 94</html> 95