blob: a7cbbfdf454e322cb8f16e45e918f476bb81205f [file] [log] [blame]
Wenzel Jakobde623a72016-03-09 21:11:19 +01001Frequently asked questions
2##########################
3
Wenzel Jakobc62360d2016-05-03 14:32:47 +02004"ImportError: dynamic module does not define init function"
5===========================================================
Wenzel Jakob50ed3612016-04-11 17:38:25 +02006
Ahuva Kroizer8f5b7fc2018-11-13 14:25:57 +020071. Make sure that the name specified in PYBIND11_MODULE is identical to the
8filename of the extension library (without prefixes such as .so)
9
102. If the above did not fix the issue, you are likely using an incompatible
11version of Python (for instance, the extension library was compiled against
12Python 2, while the interpreter is running on top of some version of Python
133, or vice versa).
Wenzel Jakobc62360d2016-05-03 14:32:47 +020014
15"Symbol not found: ``__Py_ZeroStruct`` / ``_PyInstanceMethod_Type``"
16========================================================================
17
Dean Moldovan443ab592017-04-24 01:51:44 +020018See the first answer.
Wenzel Jakobc62360d2016-05-03 14:32:47 +020019
Wenzel Jakob70f5a4d2016-09-05 17:19:18 +090020"SystemError: dynamic module not initialized properly"
21======================================================
22
Dean Moldovan443ab592017-04-24 01:51:44 +020023See the first answer.
Wenzel Jakob70f5a4d2016-09-05 17:19:18 +090024
Wenzel Jakobc62360d2016-05-03 14:32:47 +020025The Python interpreter immediately crashes when importing my module
26===================================================================
27
Dean Moldovan443ab592017-04-24 01:51:44 +020028See the first answer.
Wenzel Jakob50ed3612016-04-11 17:38:25 +020029
Wenzel Jakobf600c1d2016-06-03 14:47:54 +020030CMake doesn't detect the right Python version
31=============================================
Wenzel Jakoba439cca2016-05-17 10:47:52 +020032
Wenzel Jakobf600c1d2016-06-03 14:47:54 +020033The CMake-based build system will try to automatically detect the installed
34version of Python and link against that. When this fails, or when there are
35multiple versions of Python and it finds the wrong one, delete
36``CMakeCache.txt`` and then invoke CMake as follows:
Wenzel Jakoba439cca2016-05-17 10:47:52 +020037
38.. code-block:: bash
39
Wenzel Jakobf600c1d2016-06-03 14:47:54 +020040 cmake -DPYTHON_EXECUTABLE:FILEPATH=<path-to-python-executable> .
Wenzel Jakoba439cca2016-05-17 10:47:52 +020041
Ivor Wanders2b045752019-06-10 16:12:28 -040042.. _faq_reference_arguments:
43
Wenzel Jakobde623a72016-03-09 21:11:19 +010044Limitations involving reference arguments
45=========================================
46
47In C++, it's fairly common to pass arguments using mutable references or
48mutable pointers, which allows both read and write access to the value
49supplied by the caller. This is sometimes done for efficiency reasons, or to
50realize functions that have multiple return values. Here are two very basic
51examples:
52
53.. code-block:: cpp
54
55 void increment(int &i) { i++; }
56 void increment_ptr(int *i) { (*i)++; }
57
58In Python, all arguments are passed by reference, so there is no general
59issue in binding such code from Python.
60
61However, certain basic Python types (like ``str``, ``int``, ``bool``,
62``float``, etc.) are **immutable**. This means that the following attempt
63to port the function to Python doesn't have the same effect on the value
64provided by the caller -- in fact, it does nothing at all.
65
66.. code-block:: python
67
68 def increment(i):
69 i += 1 # nope..
70
Wenzel Jakob4a48afb2016-03-09 21:31:21 +010071pybind11 is also affected by such language-level conventions, which means that
72binding ``increment`` or ``increment_ptr`` will also create Python functions
73that don't modify their arguments.
Wenzel Jakobde623a72016-03-09 21:11:19 +010074
Wenzel Jakob4a48afb2016-03-09 21:31:21 +010075Although inconvenient, one workaround is to encapsulate the immutable types in
Wenzel Jakob2e03a582016-04-14 11:27:15 +020076a custom type that does allow modifications.
Wenzel Jakob4a48afb2016-03-09 21:31:21 +010077
78An other alternative involves binding a small wrapper lambda function that
79returns a tuple with all output arguments (see the remainder of the
80documentation for examples on binding lambda functions). An example:
81
82.. code-block:: cpp
83
84 int foo(int &i) { i++; return 123; }
85
86and the binding code
87
88.. code-block:: cpp
89
90 m.def("foo", [](int i) { int rv = foo(i); return std::make_tuple(rv, i); });
91
Wenzel Jakob2e03a582016-04-14 11:27:15 +020092
Wenzel Jakobc79dbe42016-04-17 21:54:31 +020093How can I reduce the build time?
94================================
95
Jason Rhinelanderfb7c9fd2016-10-22 12:54:33 -040096It's good practice to split binding code over multiple files, as in the
97following example:
98
99:file:`example.cpp`:
Wenzel Jakobc79dbe42016-04-17 21:54:31 +0200100
101.. code-block:: cpp
102
103 void init_ex1(py::module &);
104 void init_ex2(py::module &);
105 /* ... */
106
Dean Moldovan443ab592017-04-24 01:51:44 +0200107 PYBIND11_MODULE(example, m) {
Wenzel Jakobc79dbe42016-04-17 21:54:31 +0200108 init_ex1(m);
109 init_ex2(m);
Wenzel Jakobc79dbe42016-04-17 21:54:31 +0200110 /* ... */
Wenzel Jakobc79dbe42016-04-17 21:54:31 +0200111 }
112
Jason Rhinelanderfb7c9fd2016-10-22 12:54:33 -0400113:file:`ex1.cpp`:
114
115.. code-block:: cpp
116
117 void init_ex1(py::module &m) {
118 m.def("add", [](int a, int b) { return a + b; });
119 }
120
121:file:`ex2.cpp`:
122
123.. code-block:: cpp
124
David Caron307ea6b2018-04-24 10:16:18 -0400125 void init_ex2(py::module &m) {
Jason Rhinelanderfb7c9fd2016-10-22 12:54:33 -0400126 m.def("sub", [](int a, int b) { return a - b; });
127 }
128
129:command:`python`:
130
131.. code-block:: pycon
132
133 >>> import example
134 >>> example.add(1, 2)
135 3
136 >>> example.sub(1, 1)
137 0
138
139As shown above, the various ``init_ex`` functions should be contained in
140separate files that can be compiled independently from one another, and then
141linked together into the same final shared object. Following this approach
142will:
Wenzel Jakobc79dbe42016-04-17 21:54:31 +0200143
Wenzel Jakobf64feaf2016-04-28 14:33:45 +02001441. reduce memory requirements per compilation unit.
Wenzel Jakobc79dbe42016-04-17 21:54:31 +0200145
Wenzel Jakobf64feaf2016-04-28 14:33:45 +02001462. enable parallel builds (if desired).
Wenzel Jakobc79dbe42016-04-17 21:54:31 +0200147
Wenzel Jakobf64feaf2016-04-28 14:33:45 +02001483. allow for faster incremental builds. For instance, when a single class
Jason Rhinelander20ef6262016-09-21 13:39:02 -0400149 definition is changed, only a subset of the binding code will generally need
Wenzel Jakobf64feaf2016-04-28 14:33:45 +0200150 to be recompiled.
Wenzel Jakobc79dbe42016-04-17 21:54:31 +0200151
Wenzel Jakob2fb5f1d2016-11-16 17:36:54 +0100152"recursive template instantiation exceeded maximum depth of 256"
153================================================================
154
155If you receive an error about excessive recursive template evaluation, try
156specifying a larger value, e.g. ``-ftemplate-depth=1024`` on GCC/Clang. The
157culprit is generally the generation of function signatures at compile time
158using C++14 template metaprogramming.
159
Jason Rhinelander97aa54f2017-08-10 12:08:42 -0400160.. _`faq:hidden_visibility`:
161
162"‘SomeClass’ declared with greater visibility than the type of its field ‘SomeClass::member’ [-Wattributes]"
163============================================================================================================
164
165This error typically indicates that you are compiling without the required
166``-fvisibility`` flag. pybind11 code internally forces hidden visibility on
167all internal code, but if non-hidden (and thus *exported*) code attempts to
168include a pybind type (for example, ``py::object`` or ``py::list``) you can run
169into this warning.
170
171To avoid it, make sure you are specifying ``-fvisibility=hidden`` when
172compiling pybind code.
173
174As to why ``-fvisibility=hidden`` is necessary, because pybind modules could
175have been compiled under different versions of pybind itself, it is also
176important that the symbols defined in one module do not clash with the
177potentially-incompatible symbols defined in another. While Python extension
178modules are usually loaded with localized symbols (under POSIX systems
179typically using ``dlopen`` with the ``RTLD_LOCAL`` flag), this Python default
180can be changed, but even if it isn't it is not always enough to guarantee
181complete independence of the symbols involved when not using
182``-fvisibility=hidden``.
183
184Additionally, ``-fvisiblity=hidden`` can deliver considerably binary size
185savings. (See the following section for more details).
186
Wenzel Jakob2fb5f1d2016-11-16 17:36:54 +0100187
Lori A. Burns5cafc992016-12-13 10:55:38 -0500188.. _`faq:symhidden`:
189
Wenzel Jakobc79dbe42016-04-17 21:54:31 +0200190How can I create smaller binaries?
191==================================
192
193To do its job, pybind11 extensively relies on a programming technique known as
194*template metaprogramming*, which is a way of performing computation at compile
195time using type information. Template metaprogamming usually instantiates code
196involving significant numbers of deeply nested types that are either completely
Jason Rhinelander20ef6262016-09-21 13:39:02 -0400197removed or reduced to just a few instructions during the compiler's optimization
Wenzel Jakobc79dbe42016-04-17 21:54:31 +0200198phase. However, due to the nested nature of these types, the resulting symbol
199names in the compiled extension library can be extremely long. For instance,
200the included test suite contains the following symbol:
201
Wenzel Jakobf64feaf2016-04-28 14:33:45 +0200202.. only:: html
Wenzel Jakobc79dbe42016-04-17 21:54:31 +0200203
Wenzel Jakobf64feaf2016-04-28 14:33:45 +0200204 .. code-block:: none
205
Wenzel Jakobfe342412016-09-06 13:02:29 +0900206 _​_​Z​N​8​p​y​b​i​n​d​1​1​1​2​c​p​p​_​f​u​n​c​t​i​o​n​C​1​I​v​8​E​x​a​m​p​l​e​2​J​R​N​S​t​3​_​_​1​6​v​e​c​t​o​r​I​N​S​3​_​1​2​b​a​s​i​c​_​s​t​r​i​n​g​I​w​N​S​3​_​1​1​c​h​a​r​_​t​r​a​i​t​s​I​w​E​E​N​S​3​_​9​a​l​l​o​c​a​t​o​r​I​w​E​E​E​E​N​S​8​_​I​S​A​_​E​E​E​E​E​J​N​S​_​4​n​a​m​e​E​N​S​_​7​s​i​b​l​i​n​g​E​N​S​_​9​i​s​_​m​e​t​h​o​d​E​A​2​8​_​c​E​E​E​M​T​0​_​F​T​_​D​p​T​1​_​E​D​p​R​K​T​2​_
Wenzel Jakobf64feaf2016-04-28 14:33:45 +0200207
208.. only:: not html
209
210 .. code-block:: cpp
211
212 __ZN8pybind1112cpp_functionC1Iv8Example2JRNSt3__16vectorINS3_12basic_stringIwNS3_11char_traitsIwEENS3_9allocatorIwEEEENS8_ISA_EEEEEJNS_4nameENS_7siblingENS_9is_methodEA28_cEEEMT0_FT_DpT1_EDpRKT2_
Wenzel Jakobc79dbe42016-04-17 21:54:31 +0200213
214which is the mangled form of the following function type:
215
216.. code-block:: cpp
217
218 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])
219
Wenzel Jakobf64feaf2016-04-28 14:33:45 +0200220The memory needed to store just the mangled name of this function (196 bytes)
221is larger than the actual piece of code (111 bytes) it represents! On the other
222hand, it's silly to even give this function a name -- after all, it's just a
223tiny cog in a bigger piece of machinery that is not exposed to the outside
224world. So we'll generally only want to export symbols for those functions which
225are actually called from the outside.
Wenzel Jakobc79dbe42016-04-17 21:54:31 +0200226
227This can be achieved by specifying the parameter ``-fvisibility=hidden`` to GCC
Jason Rhinelander97aa54f2017-08-10 12:08:42 -0400228and Clang, which sets the default symbol visibility to *hidden*, which has a
229tremendous impact on the final binary size of the resulting extension library.
230(On Visual Studio, symbols are already hidden by default, so nothing needs to
231be done there.)
232
233In addition to decreasing binary size, ``-fvisibility=hidden`` also avoids
234potential serious issues when loading multiple modules and is required for
235proper pybind operation. See the previous FAQ entry for more details.
Wenzel Jakobc79dbe42016-04-17 21:54:31 +0200236
Marc Schlaichab003db2018-02-06 16:07:27 +0100237Working with ancient Visual Studio 2008 builds on Windows
Wenzel Jakobc62360d2016-05-03 14:32:47 +0200238=========================================================
239
240The official Windows distributions of Python are compiled using truly
241ancient versions of Visual Studio that lack good C++11 support. Some users
242implicitly assume that it would be impossible to load a plugin built with
243Visual Studio 2015 into a Python distribution that was compiled using Visual
Marc Schlaichab003db2018-02-06 16:07:27 +0100244Studio 2008. However, no such issue exists: it's perfectly legitimate to
Wenzel Jakobc62360d2016-05-03 14:32:47 +0200245interface DLLs that are built with different compilers and/or C libraries.
246Common gotchas to watch out for involve not ``free()``-ing memory region
247that that were ``malloc()``-ed in another shared library, using data
248structures with incompatible ABIs, and so on. pybind11 is very careful not
249to make these types of mistakes.
Wenzel Jakobe7d304f2017-11-17 18:44:20 +0100250
Charles Brossollet0f1d3bf2019-11-25 10:59:53 +0100251How can I properly handle Ctrl-C in long-running functions?
252===========================================================
253
254Ctrl-C is received by the Python interpreter, and holds it until the GIL
255is released, so a long-running function won't be interrupted.
256
257To interrupt from inside your function, you can use the ``PyErr_CheckSignals()``
258function, that will tell if a signal has been raised on the Python side. This
259function merely checks a flag, so its impact is negligible. When a signal has
260been received, you can explicitely interrupt execution by throwing an exception
261that gets translated to KeyboardInterrupt (see :doc:`advanced/exceptions`
262section):
263
264.. code-block:: cpp
265
266 class interruption_error: public std::exception {
267 public:
268 const char* what() const noexcept {
269 return "Interruption signal caught.";
270 }
271 };
272
273 PYBIND11_MODULE(example, m)
274 {
275 m.def("long running_func", []()
276 {
277 for (;;) {
278 if (PyErr_CheckSignals() != 0)
279 throw interruption_error();
280 // Long running iteration
281 }
282 });
283 py::register_exception<interruption_error>(m, "KeyboardInterrupt");
284 }
285
Semen Yesylevskyyef13fb22018-09-12 01:20:56 +0300286Inconsistent detection of Python version in CMake and pybind11
287==============================================================
288
289The functions ``find_package(PythonInterp)`` and ``find_package(PythonLibs)`` provided by CMake
290for Python version detection are not used by pybind11 due to unreliability and limitations that make
291them unsuitable for pybind11's needs. Instead pybind provides its own, more reliable Python detection
292CMake code. Conflicts can arise, however, when using pybind11 in a project that *also* uses the CMake
293Python detection in a system with several Python versions installed.
294
295This difference may cause inconsistencies and errors if *both* mechanisms are used in the same project. Consider the following
296Cmake code executed in a system with Python 2.7 and 3.x installed:
297
298.. code-block:: cmake
299
300 find_package(PythonInterp)
301 find_package(PythonLibs)
302 find_package(pybind11)
303
304It will detect Python 2.7 and pybind11 will pick it as well.
305
306In contrast this code:
307
308.. code-block:: cmake
309
310 find_package(pybind11)
311 find_package(PythonInterp)
312 find_package(PythonLibs)
313
314will detect Python 3.x for pybind11 and may crash on ``find_package(PythonLibs)`` afterwards.
315
316It is advised to avoid using ``find_package(PythonInterp)`` and ``find_package(PythonLibs)`` from CMake and rely
317on pybind11 in detecting Python version. If this is not possible CMake machinery should be called *before* including pybind11.
318
Wenzel Jakobe7d304f2017-11-17 18:44:20 +0100319How to cite this project?
320=========================
321
322We suggest the following BibTeX template to cite pybind11 in scientific
323discourse:
324
325.. code-block:: bash
326
327 @misc{pybind11,
328 author = {Wenzel Jakob and Jason Rhinelander and Dean Moldovan},
329 year = {2017},
330 note = {https://github.com/pybind/pybind11},
331 title = {pybind11 -- Seamless operability between C++11 and Python}
332 }