boost_python_import_hpp.html 6.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  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&#160;6.&#160;Embedding">
  9. <link rel="prev" href="../embedding.html" title="Chapter&#160;6.&#160;Embedding">
  10. <link rel="next" href="../utility_and_infrastructure.html" title="Chapter&#160;7.&#160;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">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
  65. <span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;</span>
  66. <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>
  67. <span class="keyword">void</span> <span class="identifier">print_python_version</span><span class="special">()</span>
  68. <span class="special">{</span>
  69. <span class="comment">// Load the sys module.</span>
  70. <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>
  71. <span class="comment">// Extract the python version.</span>
  72. <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">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;(</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>
  73. <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">version</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
  74. <span class="special">}</span>
  75. </pre>
  76. </div>
  77. </div>
  78. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  79. <td align="left"></td>
  80. <td align="right"><div class="copyright-footer">Copyright &#169; 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
  81. Distributed under the Boost Software License, Version 1.0. (See accompanying
  82. 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>
  83. </p>
  84. </div></td>
  85. </tr></table>
  86. <hr>
  87. <div class="spirit-nav">
  88. <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>
  89. </div>
  90. </body>
  91. </html>