2020-05-05, 19:28
Update: So I have a tentative fix. I see that when loaded as a library module, the path to the lib folder is added to the sys.path, but when started as a script it isn't. So I did a sys.path.insert to insert the path to "lib" at the beginning of the sys.path which I think approximates how py2 worked?
---------------------------------------------------------------------------------------------------------
I've been converting some Py2 scripts to to Py3 and my biggest problem has been importing modules. One has me stumped.
The addon works both as a library and as its own addon via its addon.xml:
The folder structure is
- control.py
-lib
--__init__.py
-- module1.py
--module2.py
--more modules.py
--subfolders etc
The problem is for example in module1 it imports module2 or other modules from the subfolders via
So when I first converted it and imported module1 in the lib folder from other addon that worked (module1 could import module2). But when I ran it as a script I got module exceptions. So naively I patched it by changing imports to relative imports like
. That fixed it when run as a script, but now it won't work when importing within the other script (can't resolve relative import).
I'm thinking there is some absolute path I could provide for the import that works in both cases, but can't figure out how to do that. Reading online there's plenty of info but I don't think the examples cover the case on how paths are managed within Kodi python.
It seems like "lib" is added to the path search space when used as a library module, but not when called directly? And if so, if many library addons use "lib" how does that work?
scott s.
.
---------------------------------------------------------------------------------------------------------
I've been converting some Py2 scripts to to Py3 and my biggest problem has been importing modules. One has me stumped.
The addon works both as a library and as its own addon via its addon.xml:
Code:
<extension point="xbmc.python.script" library="control.py">
<provides>executable</provides>
</extension>
<extension point="xbmc.python.module" library="lib" />
The folder structure is
- control.py
-lib
--__init__.py
-- module1.py
--module2.py
--more modules.py
--subfolders etc
The problem is for example in module1 it imports module2 or other modules from the subfolders via
Code:
import module2
So when I first converted it and imported module1 in the lib folder from other addon that worked (module1 could import module2). But when I ran it as a script I got module exceptions. So naively I patched it by changing imports to relative imports like
Code:
from . import module2
I'm thinking there is some absolute path I could provide for the import that works in both cases, but can't figure out how to do that. Reading online there's plenty of info but I don't think the examples cover the case on how paths are managed within Kodi python.
It seems like "lib" is added to the path search space when used as a library module, but not when called directly? And if so, if many library addons use "lib" how does that work?
scott s.
.