Python question ... sorry :-)

I googled this but not finding a good solution so I thought I’d ask here …

I have two libraries that require functions from each other … I can’t include them in each other without creating a circular definition …

For now, I’m just repeating the library function where I need it, which is sub-optimal. Maybe it’s a matter of rethinking whats in what library module?

I’m assuming someone else here has run across this. It’s making my head hurt … what am I missing?

Just include one at the bottom of the other.
That way you still can include the function.

Thanks Sebastian … Unfortunately I can’t follow you … can you be a little more explicit?

file_a:

from file_b import func_b

def func a():
    func_b()

file_b:

def func_b():
    func_a()

from file_a import func_a

Well, ok I tried that … and it still gives an error (using your example as a test)

2022-07-18 11:18:42.173 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab/automation/jsr223/python/personal/test.py': ImportError: cannot import name func_a in /etc/openhab/automation/jsr223/python/personal/test.py at line number 1

test_a.py contains:

import personal.file_a
reload(personal.file_a)
from personal.file_a import func_a

Just to be clear, both file_a and file_b are in the library … test.py is in personal space.

At least that’s the way how it can be done in plain python.
The error is for test.py but you are showing the contents of test_a.py

Something like this will work:

def func a():
    from file_b import func_b
    func_b()

result is then:

2022-07-19 09:18:04.413 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/openhab/conf/automation/jsr223/python/personal/test_circ.py': RuntimeError: maximum recursion depth exceeded (Java StackOverflowError) in /openhab/conf/automation/jsr223/python/personal/test_circ.py at line number 6

more ideas can be found here:

sorry … that was just a typo … test.py importing from file_a or file_b has the same error.

Thanks @lukics … I was hoping I was just missing something (other than bad design) … I think the easiest solution is to add a 3rd library that contains the common function. :slight_smile:

Thanks for your responses @Spaceman_Spiff and @lukics