开发者

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

开发者 https://www.devze.com 2023-02-23 01:19 出处:网络
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 th

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 ma开发者_开发百科ke 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.


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


I was able to get debugging working in native code in Python extension: with mix of debug and release python libs: but I used some hacks:

  1. build your native library in "Release mode with debug info": see https://learn.microsoft.com/en-us/cpp/build/how-to-debug-a-release-build?view=vs-2019
  2. run python.exe setup.py build_ext --compiler=msvc --inplace on your module to build your extension. This is just to view the compiler toolchain commands
  3. copy and paste these commands to a .bat file
  4. modify the commands: enable debug symbols (/Zi), disable optimization (/Od), etc.

change /Ox to /Od
add /Zi flag to compiler flags
add /DEBUG flag to linker flags

  1. now build your extension manually instead of using python distutils

TODO: integrate these changes into Python distutils: add a "--build --relWithDebInfo" flag for visual studio toolchain

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号