Help with writing a plugin: imports

Hi. I’m writing a plugin, and I’m having trouble with the import statements.

All the Python files are in one directory, C:\Users\Bob\Documents\KiCad\6.0\scripting\plugins.

__init__.py has from .kicadverilog_action import KiCadVerilogAction. The file kicadverilog_action.py is successfully imported.

kicadverilog_action.py has import .kvgui. kvgui.py, in the same directory, is not imported. I get the error “No module named kvgui”.

If I change the import to import kvgui (no relative addressing) I still get the same error.

Why does relative addressing work in __init__.py but not in the file it imported? More importantly, how do I fix it?

(Admittedly, I am new to Python and to writing KiCad plugins.)

Thank you.

I’m not a Kicad file expert but, I’ve written about 30 plugins for my own use and they all work and have been since v5. BUT, the init.py in all of my past and current ‘init’ files are completely empty of any code/imports. They’re just blank files. Thus, try deleting code from them…

As I recall (and could be wrong) the init file is for Complex plugins, I’ve done only one Complex plugin (I prefer ‘Simple’ plugins… Look into both types of plugins…

Here’s Complex plugin info, notice it refers to init contents…

import kvgui is expecting to find a module (subfolder with its own __init__.py) I think.

You need to do from .kvgui import <whatever> to grab content from files next to the current file

Further reading: Relative imports in Python 3 - Stack Overflow

Yes, I’m doing a complex plugin, and my _init_.py matches what the docs say it should.

Alas, from .kvgui import still produces the same error.

I think I’ve found the solution, though. from . import kvgui works. I don’t know if there’s a way to import only certain names from kvgui when doing this. But at least I can make the code work.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.