Addon absolute imports not working for local packages

Hi, guys.
I’m having trouble importing local packages in my addon using absolute imports. Relative imports work, but I need some classes to use absolute imports only, as they need to be imported dynamically.

Any advice on how to handle this?

I’m developing an addon using the Blender Development extension for VSCode. I can’t manage to import the src package in the root __init__.py using absolute imports (from src import *). I also can’t import anything from factories that I need to use inside the plugin_cameras package.

Has anyone encountered this issue before?

So, while investigating the import error, I noticed that the script isn’t actually the __main__ script but rather a dynamically imported module. This means that unless sys.path includes the addon folder, I can’t use absolute imports for the local modules. I tried adding the addon folder to sys.path, but it didn’t work.

Would setting up sys.path in this way typically solve the issue? How should I configure it properly?

Maybe i could ask the user to set a new python path to the addon folder?

There are 3 options:

  • hack with sys.path so import will recognize your modules. But you’ll get bunch of warnings in addon preferences and Blender probably won’t approve your extension if you try to post it on extensions.blender.org

  • bundle those modules as wheels, then Blender will install them as dependencies and they will be accessible by absolute paths too

  • bundle a stub wheel that will redirect all accesses to itself to the actual modules in the extensions folder. Got it working one day - https://github.com/IfcOpenShell/IfcOpenShell/pull/5021#issuecomment-2233800016 but it still had the same problem as the first solution - Blender warnings. But not sure if those warnings would be considered a Blender bug or there is some other way to create this kind of stub file and not to trigger them.