import.qbk 794 B

12345678910111213141516171819202122232425262728293031
  1. [section boost/python/import.hpp]
  2. [section Introduction]
  3. Exposes a mechanism for importing python modules.
  4. [endsect]
  5. [section Function `import`]
  6. ``object import(str name);``
  7. [variablelist
  8. [[Effects][Imports the module named by name.]]
  9. [[Returns][An instance of object which holds a reference to the imported module.]]
  10. ]
  11. [endsect]
  12. [section Examples]
  13. The following example demonstrates the use of import to access a function in python, and later call it from within C++.
  14. ``
  15. #include <iostream>
  16. #include <string>
  17. using namespace boost::python;
  18. void print_python_version()
  19. {
  20. // Load the sys module.
  21. object sys = import("sys");
  22. // Extract the python version.
  23. std::string version = extract<std::string>(sys.attr("version"));
  24. std::cout << version << std::endl;
  25. }
  26. ``
  27. [endsect]
  28. [endsect]