You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.9 KiB

  1. Frequently asked questions
  2. ##########################
  3. (under construction)
  4. ImportError: dynamic module does not define init function
  5. =========================================================
  6. 1. Make sure that the name specified in ``pybind::module`` and
  7. ``PYBIND11_PLUGIN`` is consistent and identical to the filename of the
  8. extension library. The latter should not contain any extra prefixes (e.g.
  9. ``test.so`` instead of ``libtest.so``).
  10. 2. If the above did not fix your issue, then you are likely using an
  11. incompatible version of Python (for instance, the extension library was
  12. compiled against Python 2, while the interpreter is running on top of some
  13. version of Python 3)
  14. Limitations involving reference arguments
  15. =========================================
  16. In C++, it's fairly common to pass arguments using mutable references or
  17. mutable pointers, which allows both read and write access to the value
  18. supplied by the caller. This is sometimes done for efficiency reasons, or to
  19. realize functions that have multiple return values. Here are two very basic
  20. examples:
  21. .. code-block:: cpp
  22. void increment(int &i) { i++; }
  23. void increment_ptr(int *i) { (*i)++; }
  24. In Python, all arguments are passed by reference, so there is no general
  25. issue in binding such code from Python.
  26. However, certain basic Python types (like ``str``, ``int``, ``bool``,
  27. ``float``, etc.) are **immutable**. This means that the following attempt
  28. to port the function to Python doesn't have the same effect on the value
  29. provided by the caller -- in fact, it does nothing at all.
  30. .. code-block:: python
  31. def increment(i):
  32. i += 1 # nope..
  33. pybind11 is also affected by such language-level conventions, which means that
  34. binding ``increment`` or ``increment_ptr`` will also create Python functions
  35. that don't modify their arguments.
  36. Although inconvenient, one workaround is to encapsulate the immutable types in
  37. a custom type that does allow modifications.
  38. An other alternative involves binding a small wrapper lambda function that
  39. returns a tuple with all output arguments (see the remainder of the
  40. documentation for examples on binding lambda functions). An example:
  41. .. code-block:: cpp
  42. int foo(int &i) { i++; return 123; }
  43. and the binding code
  44. .. code-block:: cpp
  45. m.def("foo", [](int i) { int rv = foo(i); return std::make_tuple(rv, i); });
  46. CMake doesn't detect the right Python version, or it finds mismatched interpreter and library versions
  47. ======================================================================================================
  48. The Python detection logic of CMake is flawed and can sometimes fail to find
  49. the desired Python version, or it chooses mismatched interpreter and library
  50. versions. A longer discussion is available on the pybind11 issue tracker
  51. [#f1]_, though this is ultimately not a pybind11 issue.
  52. To force the build system to choose a particular version, delete CMakeCache.txt
  53. and then invoke CMake as follows:
  54. .. code-block:: bash
  55. cmake -DPYTHON_EXECUTABLE:FILEPATH=<...> \
  56. -DPYTHON_LIBRARY:FILEPATH=<...> \
  57. -DPYTHON_INCLUDE_DIR:PATH=<...> .
  58. .. [#f1] http://github.com/pybind/pybind11/issues/99
  59. Working with ancient Visual Studio 2009 builds on Windows
  60. =========================================================
  61. The official Windows distributions of Python are compiled using truly
  62. ancient versions of Visual Studio that lack good C++11 support. Some users
  63. implicitly assume that it would be impossible to load a plugin built with
  64. Visual Studio 2015 into a Python distribution that was compiled using Visual
  65. Studio 2009. However, no such issue exists: it's perfectly legitimate to
  66. interface DLLs that are built with different compilers and/or C libraries.
  67. Common gotchas to watch out for involve not ``free()``-ing memory region
  68. that that were ``malloc()``-ed in another shared library, using data
  69. structures with incompatible ABIs, and so on. pybind11 is very careful not
  70. to make these types of mistakes.