Wenzel Jakob | de623a7 | 2016-03-09 21:11:19 +0100 | [diff] [blame] | 1 | Frequently asked questions |
| 2 | ########################## |
| 3 | |
Wenzel Jakob | c62360d | 2016-05-03 14:32:47 +0200 | [diff] [blame] | 4 | "ImportError: dynamic module does not define init function" |
| 5 | =========================================================== |
Wenzel Jakob | 50ed361 | 2016-04-11 17:38:25 +0200 | [diff] [blame] | 6 | |
Ahuva Kroizer | 8f5b7fc | 2018-11-13 14:25:57 +0200 | [diff] [blame^] | 7 | 1. Make sure that the name specified in PYBIND11_MODULE is identical to the |
| 8 | filename of the extension library (without prefixes such as .so) |
| 9 | |
| 10 | 2. If the above did not fix the issue, you are likely using an incompatible |
| 11 | version of Python (for instance, the extension library was compiled against |
| 12 | Python 2, while the interpreter is running on top of some version of Python |
| 13 | 3, or vice versa). |
Wenzel Jakob | c62360d | 2016-05-03 14:32:47 +0200 | [diff] [blame] | 14 | |
| 15 | "Symbol not found: ``__Py_ZeroStruct`` / ``_PyInstanceMethod_Type``" |
| 16 | ======================================================================== |
| 17 | |
Dean Moldovan | 443ab59 | 2017-04-24 01:51:44 +0200 | [diff] [blame] | 18 | See the first answer. |
Wenzel Jakob | c62360d | 2016-05-03 14:32:47 +0200 | [diff] [blame] | 19 | |
Wenzel Jakob | 70f5a4d | 2016-09-05 17:19:18 +0900 | [diff] [blame] | 20 | "SystemError: dynamic module not initialized properly" |
| 21 | ====================================================== |
| 22 | |
Dean Moldovan | 443ab59 | 2017-04-24 01:51:44 +0200 | [diff] [blame] | 23 | See the first answer. |
Wenzel Jakob | 70f5a4d | 2016-09-05 17:19:18 +0900 | [diff] [blame] | 24 | |
Wenzel Jakob | c62360d | 2016-05-03 14:32:47 +0200 | [diff] [blame] | 25 | The Python interpreter immediately crashes when importing my module |
| 26 | =================================================================== |
| 27 | |
Dean Moldovan | 443ab59 | 2017-04-24 01:51:44 +0200 | [diff] [blame] | 28 | See the first answer. |
Wenzel Jakob | 50ed361 | 2016-04-11 17:38:25 +0200 | [diff] [blame] | 29 | |
Wenzel Jakob | f600c1d | 2016-06-03 14:47:54 +0200 | [diff] [blame] | 30 | CMake doesn't detect the right Python version |
| 31 | ============================================= |
Wenzel Jakob | a439cca | 2016-05-17 10:47:52 +0200 | [diff] [blame] | 32 | |
Wenzel Jakob | f600c1d | 2016-06-03 14:47:54 +0200 | [diff] [blame] | 33 | The CMake-based build system will try to automatically detect the installed |
| 34 | version of Python and link against that. When this fails, or when there are |
| 35 | multiple versions of Python and it finds the wrong one, delete |
| 36 | ``CMakeCache.txt`` and then invoke CMake as follows: |
Wenzel Jakob | a439cca | 2016-05-17 10:47:52 +0200 | [diff] [blame] | 37 | |
| 38 | .. code-block:: bash |
| 39 | |
Wenzel Jakob | f600c1d | 2016-06-03 14:47:54 +0200 | [diff] [blame] | 40 | cmake -DPYTHON_EXECUTABLE:FILEPATH=<path-to-python-executable> . |
Wenzel Jakob | a439cca | 2016-05-17 10:47:52 +0200 | [diff] [blame] | 41 | |
Wenzel Jakob | de623a7 | 2016-03-09 21:11:19 +0100 | [diff] [blame] | 42 | Limitations involving reference arguments |
| 43 | ========================================= |
| 44 | |
| 45 | In C++, it's fairly common to pass arguments using mutable references or |
| 46 | mutable pointers, which allows both read and write access to the value |
| 47 | supplied by the caller. This is sometimes done for efficiency reasons, or to |
| 48 | realize functions that have multiple return values. Here are two very basic |
| 49 | examples: |
| 50 | |
| 51 | .. code-block:: cpp |
| 52 | |
| 53 | void increment(int &i) { i++; } |
| 54 | void increment_ptr(int *i) { (*i)++; } |
| 55 | |
| 56 | In Python, all arguments are passed by reference, so there is no general |
| 57 | issue in binding such code from Python. |
| 58 | |
| 59 | However, certain basic Python types (like ``str``, ``int``, ``bool``, |
| 60 | ``float``, etc.) are **immutable**. This means that the following attempt |
| 61 | to port the function to Python doesn't have the same effect on the value |
| 62 | provided by the caller -- in fact, it does nothing at all. |
| 63 | |
| 64 | .. code-block:: python |
| 65 | |
| 66 | def increment(i): |
| 67 | i += 1 # nope.. |
| 68 | |
Wenzel Jakob | 4a48afb | 2016-03-09 21:31:21 +0100 | [diff] [blame] | 69 | pybind11 is also affected by such language-level conventions, which means that |
| 70 | binding ``increment`` or ``increment_ptr`` will also create Python functions |
| 71 | that don't modify their arguments. |
Wenzel Jakob | de623a7 | 2016-03-09 21:11:19 +0100 | [diff] [blame] | 72 | |
Wenzel Jakob | 4a48afb | 2016-03-09 21:31:21 +0100 | [diff] [blame] | 73 | Although inconvenient, one workaround is to encapsulate the immutable types in |
Wenzel Jakob | 2e03a58 | 2016-04-14 11:27:15 +0200 | [diff] [blame] | 74 | a custom type that does allow modifications. |
Wenzel Jakob | 4a48afb | 2016-03-09 21:31:21 +0100 | [diff] [blame] | 75 | |
| 76 | An other alternative involves binding a small wrapper lambda function that |
| 77 | returns a tuple with all output arguments (see the remainder of the |
| 78 | documentation for examples on binding lambda functions). An example: |
| 79 | |
| 80 | .. code-block:: cpp |
| 81 | |
| 82 | int foo(int &i) { i++; return 123; } |
| 83 | |
| 84 | and the binding code |
| 85 | |
| 86 | .. code-block:: cpp |
| 87 | |
| 88 | m.def("foo", [](int i) { int rv = foo(i); return std::make_tuple(rv, i); }); |
| 89 | |
Wenzel Jakob | 2e03a58 | 2016-04-14 11:27:15 +0200 | [diff] [blame] | 90 | |
Wenzel Jakob | c79dbe4 | 2016-04-17 21:54:31 +0200 | [diff] [blame] | 91 | How can I reduce the build time? |
| 92 | ================================ |
| 93 | |
Jason Rhinelander | fb7c9fd | 2016-10-22 12:54:33 -0400 | [diff] [blame] | 94 | It's good practice to split binding code over multiple files, as in the |
| 95 | following example: |
| 96 | |
| 97 | :file:`example.cpp`: |
Wenzel Jakob | c79dbe4 | 2016-04-17 21:54:31 +0200 | [diff] [blame] | 98 | |
| 99 | .. code-block:: cpp |
| 100 | |
| 101 | void init_ex1(py::module &); |
| 102 | void init_ex2(py::module &); |
| 103 | /* ... */ |
| 104 | |
Dean Moldovan | 443ab59 | 2017-04-24 01:51:44 +0200 | [diff] [blame] | 105 | PYBIND11_MODULE(example, m) { |
Wenzel Jakob | c79dbe4 | 2016-04-17 21:54:31 +0200 | [diff] [blame] | 106 | init_ex1(m); |
| 107 | init_ex2(m); |
Wenzel Jakob | c79dbe4 | 2016-04-17 21:54:31 +0200 | [diff] [blame] | 108 | /* ... */ |
Wenzel Jakob | c79dbe4 | 2016-04-17 21:54:31 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Jason Rhinelander | fb7c9fd | 2016-10-22 12:54:33 -0400 | [diff] [blame] | 111 | :file:`ex1.cpp`: |
| 112 | |
| 113 | .. code-block:: cpp |
| 114 | |
| 115 | void init_ex1(py::module &m) { |
| 116 | m.def("add", [](int a, int b) { return a + b; }); |
| 117 | } |
| 118 | |
| 119 | :file:`ex2.cpp`: |
| 120 | |
| 121 | .. code-block:: cpp |
| 122 | |
David Caron | 307ea6b | 2018-04-24 10:16:18 -0400 | [diff] [blame] | 123 | void init_ex2(py::module &m) { |
Jason Rhinelander | fb7c9fd | 2016-10-22 12:54:33 -0400 | [diff] [blame] | 124 | m.def("sub", [](int a, int b) { return a - b; }); |
| 125 | } |
| 126 | |
| 127 | :command:`python`: |
| 128 | |
| 129 | .. code-block:: pycon |
| 130 | |
| 131 | >>> import example |
| 132 | >>> example.add(1, 2) |
| 133 | 3 |
| 134 | >>> example.sub(1, 1) |
| 135 | 0 |
| 136 | |
| 137 | As shown above, the various ``init_ex`` functions should be contained in |
| 138 | separate files that can be compiled independently from one another, and then |
| 139 | linked together into the same final shared object. Following this approach |
| 140 | will: |
Wenzel Jakob | c79dbe4 | 2016-04-17 21:54:31 +0200 | [diff] [blame] | 141 | |
Wenzel Jakob | f64feaf | 2016-04-28 14:33:45 +0200 | [diff] [blame] | 142 | 1. reduce memory requirements per compilation unit. |
Wenzel Jakob | c79dbe4 | 2016-04-17 21:54:31 +0200 | [diff] [blame] | 143 | |
Wenzel Jakob | f64feaf | 2016-04-28 14:33:45 +0200 | [diff] [blame] | 144 | 2. enable parallel builds (if desired). |
Wenzel Jakob | c79dbe4 | 2016-04-17 21:54:31 +0200 | [diff] [blame] | 145 | |
Wenzel Jakob | f64feaf | 2016-04-28 14:33:45 +0200 | [diff] [blame] | 146 | 3. allow for faster incremental builds. For instance, when a single class |
Jason Rhinelander | 20ef626 | 2016-09-21 13:39:02 -0400 | [diff] [blame] | 147 | definition is changed, only a subset of the binding code will generally need |
Wenzel Jakob | f64feaf | 2016-04-28 14:33:45 +0200 | [diff] [blame] | 148 | to be recompiled. |
Wenzel Jakob | c79dbe4 | 2016-04-17 21:54:31 +0200 | [diff] [blame] | 149 | |
Wenzel Jakob | 2fb5f1d | 2016-11-16 17:36:54 +0100 | [diff] [blame] | 150 | "recursive template instantiation exceeded maximum depth of 256" |
| 151 | ================================================================ |
| 152 | |
| 153 | If you receive an error about excessive recursive template evaluation, try |
| 154 | specifying a larger value, e.g. ``-ftemplate-depth=1024`` on GCC/Clang. The |
| 155 | culprit is generally the generation of function signatures at compile time |
| 156 | using C++14 template metaprogramming. |
| 157 | |
Jason Rhinelander | 97aa54f | 2017-08-10 12:08:42 -0400 | [diff] [blame] | 158 | .. _`faq:hidden_visibility`: |
| 159 | |
| 160 | "‘SomeClass’ declared with greater visibility than the type of its field ‘SomeClass::member’ [-Wattributes]" |
| 161 | ============================================================================================================ |
| 162 | |
| 163 | This error typically indicates that you are compiling without the required |
| 164 | ``-fvisibility`` flag. pybind11 code internally forces hidden visibility on |
| 165 | all internal code, but if non-hidden (and thus *exported*) code attempts to |
| 166 | include a pybind type (for example, ``py::object`` or ``py::list``) you can run |
| 167 | into this warning. |
| 168 | |
| 169 | To avoid it, make sure you are specifying ``-fvisibility=hidden`` when |
| 170 | compiling pybind code. |
| 171 | |
| 172 | As to why ``-fvisibility=hidden`` is necessary, because pybind modules could |
| 173 | have been compiled under different versions of pybind itself, it is also |
| 174 | important that the symbols defined in one module do not clash with the |
| 175 | potentially-incompatible symbols defined in another. While Python extension |
| 176 | modules are usually loaded with localized symbols (under POSIX systems |
| 177 | typically using ``dlopen`` with the ``RTLD_LOCAL`` flag), this Python default |
| 178 | can be changed, but even if it isn't it is not always enough to guarantee |
| 179 | complete independence of the symbols involved when not using |
| 180 | ``-fvisibility=hidden``. |
| 181 | |
| 182 | Additionally, ``-fvisiblity=hidden`` can deliver considerably binary size |
| 183 | savings. (See the following section for more details). |
| 184 | |
Wenzel Jakob | 2fb5f1d | 2016-11-16 17:36:54 +0100 | [diff] [blame] | 185 | |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 186 | .. _`faq:symhidden`: |
| 187 | |
Wenzel Jakob | c79dbe4 | 2016-04-17 21:54:31 +0200 | [diff] [blame] | 188 | How can I create smaller binaries? |
| 189 | ================================== |
| 190 | |
| 191 | To do its job, pybind11 extensively relies on a programming technique known as |
| 192 | *template metaprogramming*, which is a way of performing computation at compile |
| 193 | time using type information. Template metaprogamming usually instantiates code |
| 194 | involving significant numbers of deeply nested types that are either completely |
Jason Rhinelander | 20ef626 | 2016-09-21 13:39:02 -0400 | [diff] [blame] | 195 | removed or reduced to just a few instructions during the compiler's optimization |
Wenzel Jakob | c79dbe4 | 2016-04-17 21:54:31 +0200 | [diff] [blame] | 196 | phase. However, due to the nested nature of these types, the resulting symbol |
| 197 | names in the compiled extension library can be extremely long. For instance, |
| 198 | the included test suite contains the following symbol: |
| 199 | |
Wenzel Jakob | f64feaf | 2016-04-28 14:33:45 +0200 | [diff] [blame] | 200 | .. only:: html |
Wenzel Jakob | c79dbe4 | 2016-04-17 21:54:31 +0200 | [diff] [blame] | 201 | |
Wenzel Jakob | f64feaf | 2016-04-28 14:33:45 +0200 | [diff] [blame] | 202 | .. code-block:: none |
| 203 | |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 204 | __ZN8pybind1112cpp_functionC1Iv8Example2JRNSt3__16vectorINS3_12basic_stringIwNS3_11char_traitsIwEENS3_9allocatorIwEEEENS8_ISA_EEEEEJNS_4nameENS_7siblingENS_9is_methodEA28_cEEEMT0_FT_DpT1_EDpRKT2_ |
Wenzel Jakob | f64feaf | 2016-04-28 14:33:45 +0200 | [diff] [blame] | 205 | |
| 206 | .. only:: not html |
| 207 | |
| 208 | .. code-block:: cpp |
| 209 | |
| 210 | __ZN8pybind1112cpp_functionC1Iv8Example2JRNSt3__16vectorINS3_12basic_stringIwNS3_11char_traitsIwEENS3_9allocatorIwEEEENS8_ISA_EEEEEJNS_4nameENS_7siblingENS_9is_methodEA28_cEEEMT0_FT_DpT1_EDpRKT2_ |
Wenzel Jakob | c79dbe4 | 2016-04-17 21:54:31 +0200 | [diff] [blame] | 211 | |
| 212 | which is the mangled form of the following function type: |
| 213 | |
| 214 | .. code-block:: cpp |
| 215 | |
| 216 | pybind11::cpp_function::cpp_function<void, Example2, std::__1::vector<std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >, std::__1::allocator<std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > > >&, pybind11::name, pybind11::sibling, pybind11::is_method, char [28]>(void (Example2::*)(std::__1::vector<std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >, std::__1::allocator<std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > > >&), pybind11::name const&, pybind11::sibling const&, pybind11::is_method const&, char const (&) [28]) |
| 217 | |
Wenzel Jakob | f64feaf | 2016-04-28 14:33:45 +0200 | [diff] [blame] | 218 | The memory needed to store just the mangled name of this function (196 bytes) |
| 219 | is larger than the actual piece of code (111 bytes) it represents! On the other |
| 220 | hand, it's silly to even give this function a name -- after all, it's just a |
| 221 | tiny cog in a bigger piece of machinery that is not exposed to the outside |
| 222 | world. So we'll generally only want to export symbols for those functions which |
| 223 | are actually called from the outside. |
Wenzel Jakob | c79dbe4 | 2016-04-17 21:54:31 +0200 | [diff] [blame] | 224 | |
| 225 | This can be achieved by specifying the parameter ``-fvisibility=hidden`` to GCC |
Jason Rhinelander | 97aa54f | 2017-08-10 12:08:42 -0400 | [diff] [blame] | 226 | and Clang, which sets the default symbol visibility to *hidden*, which has a |
| 227 | tremendous impact on the final binary size of the resulting extension library. |
| 228 | (On Visual Studio, symbols are already hidden by default, so nothing needs to |
| 229 | be done there.) |
| 230 | |
| 231 | In addition to decreasing binary size, ``-fvisibility=hidden`` also avoids |
| 232 | potential serious issues when loading multiple modules and is required for |
| 233 | proper pybind operation. See the previous FAQ entry for more details. |
Wenzel Jakob | c79dbe4 | 2016-04-17 21:54:31 +0200 | [diff] [blame] | 234 | |
Marc Schlaich | ab003db | 2018-02-06 16:07:27 +0100 | [diff] [blame] | 235 | Working with ancient Visual Studio 2008 builds on Windows |
Wenzel Jakob | c62360d | 2016-05-03 14:32:47 +0200 | [diff] [blame] | 236 | ========================================================= |
| 237 | |
| 238 | The official Windows distributions of Python are compiled using truly |
| 239 | ancient versions of Visual Studio that lack good C++11 support. Some users |
| 240 | implicitly assume that it would be impossible to load a plugin built with |
| 241 | Visual Studio 2015 into a Python distribution that was compiled using Visual |
Marc Schlaich | ab003db | 2018-02-06 16:07:27 +0100 | [diff] [blame] | 242 | Studio 2008. However, no such issue exists: it's perfectly legitimate to |
Wenzel Jakob | c62360d | 2016-05-03 14:32:47 +0200 | [diff] [blame] | 243 | interface DLLs that are built with different compilers and/or C libraries. |
| 244 | Common gotchas to watch out for involve not ``free()``-ing memory region |
| 245 | that that were ``malloc()``-ed in another shared library, using data |
| 246 | structures with incompatible ABIs, and so on. pybind11 is very careful not |
| 247 | to make these types of mistakes. |
Wenzel Jakob | e7d304f | 2017-11-17 18:44:20 +0100 | [diff] [blame] | 248 | |
Semen Yesylevskyy | ef13fb2 | 2018-09-12 01:20:56 +0300 | [diff] [blame] | 249 | Inconsistent detection of Python version in CMake and pybind11 |
| 250 | ============================================================== |
| 251 | |
| 252 | The functions ``find_package(PythonInterp)`` and ``find_package(PythonLibs)`` provided by CMake |
| 253 | for Python version detection are not used by pybind11 due to unreliability and limitations that make |
| 254 | them unsuitable for pybind11's needs. Instead pybind provides its own, more reliable Python detection |
| 255 | CMake code. Conflicts can arise, however, when using pybind11 in a project that *also* uses the CMake |
| 256 | Python detection in a system with several Python versions installed. |
| 257 | |
| 258 | This difference may cause inconsistencies and errors if *both* mechanisms are used in the same project. Consider the following |
| 259 | Cmake code executed in a system with Python 2.7 and 3.x installed: |
| 260 | |
| 261 | .. code-block:: cmake |
| 262 | |
| 263 | find_package(PythonInterp) |
| 264 | find_package(PythonLibs) |
| 265 | find_package(pybind11) |
| 266 | |
| 267 | It will detect Python 2.7 and pybind11 will pick it as well. |
| 268 | |
| 269 | In contrast this code: |
| 270 | |
| 271 | .. code-block:: cmake |
| 272 | |
| 273 | find_package(pybind11) |
| 274 | find_package(PythonInterp) |
| 275 | find_package(PythonLibs) |
| 276 | |
| 277 | will detect Python 3.x for pybind11 and may crash on ``find_package(PythonLibs)`` afterwards. |
| 278 | |
| 279 | It is advised to avoid using ``find_package(PythonInterp)`` and ``find_package(PythonLibs)`` from CMake and rely |
| 280 | on pybind11 in detecting Python version. If this is not possible CMake machinery should be called *before* including pybind11. |
| 281 | |
Wenzel Jakob | e7d304f | 2017-11-17 18:44:20 +0100 | [diff] [blame] | 282 | How to cite this project? |
| 283 | ========================= |
| 284 | |
| 285 | We suggest the following BibTeX template to cite pybind11 in scientific |
| 286 | discourse: |
| 287 | |
| 288 | .. code-block:: bash |
| 289 | |
| 290 | @misc{pybind11, |
| 291 | author = {Wenzel Jakob and Jason Rhinelander and Dean Moldovan}, |
| 292 | year = {2017}, |
| 293 | note = {https://github.com/pybind/pybind11}, |
| 294 | title = {pybind11 -- Seamless operability between C++11 and Python} |
| 295 | } |