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