pthom / imgui_bundle

Dear ImGui Bundle: an extensive set of Ready-to-use widgets and libraries, based on ImGui. Start your first app in 5 lines of code, or less. Whether you prefer Python or C++, this pack has your back!
https://pthom.github.io/imgui_bundle/
MIT License
592 stars 63 forks source link

No module named 'imgui_bundle._imgui_bundle.imgui'; 'imgui_bundle._imgui_bundle' is not a package #162

Closed Aman-Anas closed 6 months ago

Aman-Anas commented 6 months ago

This is a bit of a weird usecase, but I'm testing out using imgui_bundle with UPBGE, a game engine integrated into Blender (However, I think this problem would be present for Blender addons as well). Basically, Blender retains the loaded library, and when trying to re-import imgui_bundle a second time after stopping and starting the engine runtime (in Blender-embedded mode) it fails to import imgui_bundle._imgui_bundle.imgui correctly.

It works fine in standalone player or export, so this probably has something to do with retaining state while the same process is open. Anyways, the simple fix I found was to just use a try-catch statement at the top of imgui_bundle/__init__.py to wrap the imports.

# Part of ImGui Bundle - MIT License - Copyright (c) 2022-2023 Pascal Thomet - https://github.com/pthom/imgui_bundle
try:
    from imgui_bundle._imgui_bundle import imgui as imgui  # type: ignore
    from imgui_bundle._imgui_bundle import hello_imgui as hello_imgui
    from imgui_bundle._imgui_bundle import implot as implot
    from imgui_bundle._imgui_bundle import imgui_color_text_edit as imgui_color_text_edit
    from imgui_bundle._imgui_bundle import imgui_node_editor as imgui_node_editor
    from imgui_bundle._imgui_bundle import imgui_knobs as imgui_knobs
    from imgui_bundle._imgui_bundle import im_file_dialog as im_file_dialog
    from imgui_bundle._imgui_bundle import imspinner as imspinner
    from imgui_bundle._imgui_bundle import imgui_md as imgui_md
    from imgui_bundle._imgui_bundle import immvision as immvision
    from imgui_bundle._imgui_bundle import imguizmo as imguizmo
    from imgui_bundle._imgui_bundle import imgui_tex_inspect as imgui_tex_inspect
    from imgui_bundle._imgui_bundle import imgui_toggle as imgui_toggle
    from imgui_bundle._imgui_bundle import portable_file_dialogs as portable_file_dialogs
    from imgui_bundle._imgui_bundle import imgui_command_palette as imgui_command_palette
    from imgui_bundle._imgui_bundle import im_cool_bar as im_cool_bar
    from imgui_bundle import immapp as immapp
    from imgui_bundle.immapp import icons_fontawesome as icons_fontawesome

    from imgui_bundle._imgui_bundle import __version__, compilation_time

    from imgui_bundle._imgui_bundle.imgui import ImVec2, ImVec4, ImColor, FLT_MIN, FLT_MAX  # type: ignore
    from imgui_bundle.im_col32 import IM_COL32
except ImportError:
    pass
# Glfw setup:

I'm not sure if this would break anything, and maybe there is a more correct way to do this. In my tests however, it seems to work fine and it successfully imports the module.

pthom commented 6 months ago

Hi,

Could you try to put this try/except inside the user code, i.e. near the first occurring import of imgui_bundle. It seems awkward to silently swallow error like this. I cannot make this change in production, since users may fail to see actual errors.

Aman-Anas commented 6 months ago

I was able to move the try/except logic to the user code, it's not particularly elegant but it works fine. I'm still not sure what the root cause of this issue is, but I think the fix is good enough for now. It's not as simple as wrapping the import in a try-except since the __init__.py module is called at the beginning of module initialization, so catching the error externally doesn't end up importing the module. I had to make a workaround.