Search code examples
pythondebuggingblenderpython-module

Python, load debug module from release build of Python/Blender


I have been writing a export script for Blender, which uses python for any addons. Since most of my codebase is in C++ I decided to wrap my code as a python module (pyd) which will be imported from the export script and pass all the relevant bits for conversion.

As long as I make release builds blender loads the module just fine and I can even debug with visual studio - but to resolve a bug, the release builds is not reliable so I need to use a debug build of the module. Unfortunately in that case the module doesnt load.

From python console:

 >>> import exporter_d
 Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
 ImportError: DLL load failed: The specified module could not be found.

After looking around for a bit, I find out that the error is that another dll couldnt be found and since I am not loading anything else I added the debug build of python along with my module. Now the error is different:

Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
ImportError: dynamic module does not define init function (PyInit_exporter_d)

So I changed the module init name from "PyInit_exporter" to "PyInit_exporter_d" (and removed the debug pythond.dll since it was crashing blender with a fatal error) which returns the first error (dll load faild).

So, my question is this, how can I load debug builds of a python module when running a release version of python? Since python is embedded in blender, I would like to avoid downloading the source and rebuilding it.


Solution

  • This is how to setup the environment so that you can use both debug and release build:

    In your c++ code, you need to have

    PyMODINIT_FUNC initmyExporter(void)
    

    In your Visual Studio solution (or whatever you use to specify the name of the result of compilation of your code) say

    <path_to_some_folder>\myExporter_d.pyd for Debug mode
    

    and

    <path_to_some_folder>\myExporter.pyd for Release mode
    

    When importing, use

    import myExporter
    

    with both python.exe and python_d.exe