Python,从 Python/Blender 的发布版本加载调试模块

发布于 2024-10-31 09:00:01 字数 969 浏览 0 评论 0原文

我一直在为 Blender 编写一个导出脚本,它使用 python 来处理任何插件。由于我的大部分代码库都是 C++ 语言,因此我决定将代码包装为 python 模块 (pyd),该模块将从导出脚本导入并传递所有相关位进行转换。

只要我让发布版本 Blender 加载模块就可以了,我什至可以使用 Visual Studio 进行调试 - 但为了解决错误,发布版本不可靠,所以我需要使用模块的调试版本。不幸的是,在这种情况下,模块不会加载。

从 python 控制台:

 >>> 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.

环顾四周后,我发现错误是找不到另一个 dll,并且由于我没有加载任何其他内容,所以我添加了 python 的调试版本以及我的模块。现在错误不同了:

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

所以我将模块初始化名称从“PyInit_exporter”更改为“PyInit_exporter_d”(并删除了调试 pythond.dll,因为它因致命错误而使搅拌机崩溃),这返回第一个错误(dll 加载失败)。

所以,我的问题是,在运行 python 的发布版本时,如何加载 python 模块的调试版本? 由于 python 嵌入在搅拌机中,我想避免下载源代码并重建它。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

永不分离 2024-11-07 09:00:01

这是如何设置环境,以便您可以使用调试和发布构建:

在您的 C++ 代码中,您需要

PyMODINIT_FUNC initmyExporter(void)

在 Visual Studio 解决方案中(或任何您用来指定代码编译结果的名称)说

<path_to_some_folder>\myExporter_d.pyd for Debug mode

<path_to_some_folder>\myExporter.pyd for Release mode

导入时,

import myExporter

python.exe 和 python_d.exe 一起使用

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

李白 2024-11-07 09:00:01

我能够在 Python 扩展中的本机代码中进行调试:结合调试和发布 python 库:但我使用了一些技巧:

  1. 在“带有调试信息的发布模式”下构建本机库:请参阅 https://learn.microsoft.com /en-us/cpp/build/how-to-debug-a-release-build?view=vs-2019
  2. 在模块上运行 python.exe setup.py build_ext --compiler=msvc --inplace构建您的扩展。这只是为了查看编译器工具链命令
  3. 将这些命令复制并粘贴到.bat文件中
  4. 修改命令:启用调试符号(/Zi),禁用优化(/Od)等

将/Ox更改为/Od
将 /Zi 标志添加到编译器标志
将 /DEBUG 标志添加到链接器标志

  1. 现在手动构建扩展,而不是使用 python distutils

TODO:将这些更改集成到 Python distutils 中:为 Visual Studio 工具链添加“--build --relWithDebInfo”标志

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文