Kodi Community Forum

Full Version: Import python module in kodi addon
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm figuring out how to import in kodi a python module.

addon.xml file:
https://paste.kodi.tv/cemapofapa.kodi

Folder name : script.pyxbmct.sc

Folder

Folder lib

from script.pyxbmct.sc.lib.driver import driver as dv

LOG:
https://paste.kodi.tv/ululatobop

The dots seem to confuse the addon's name. The error reports 'script.pyxbmct,' but the actual name is 'script.pyxbmct.sc.'"

How do I pass the name to Kodi?

Thank you
maybe I solved it this way after several attempts
Simply download the package from GitHub into the addon folder and import it in this way:

pack_webmanager_folder = xbmc.translatePath(os.path.join(addon_path, 'webdriver_manager'))
sys.path.append(pack_webmanager_folder)

from webdriver_manager.chrome import ChromeDriverManager


but I don't understand why, instead of calling the webmanager library installed in the addon folder, it's calling the system one as shown in the log below

https://paste.kodi.tv/vijutafiwa

I mean the line:
 File "/home/piero/.local/lib/python3.9/site-packages/webdriver_manager/....

Why don't you use the library from the addon folder?"

Thank you for your help
I checked

pack_webmanager_folder = xbmc.translatePath(os.path.join(addon_path, 'webdriver_manager'))
sys.path.append(pack_webmanager_folder)

logging.info(pack_webmanager_folder)  -> INFO:root:/home/piero/.kodi/addons/script.my.test/webdriver_manager

The path is correct
had to remove the library from the system to remove the "conflict"
The path you're headed down will work, but it isn't the Kodi way of doing python packages.  For Kodi a python package that isn't part of core python has to be repackaged as a Kodi addon, then you add it as a dependency and can import it like a "regular" python module.  If you're just doing this add on for yourself, what you're doing is fine.  But if you end up wanting to submit the thing on which you're working to the Kodi add-on repo, you'll have to package the python module the Kodi way.  Here's a list of all the python modules that have been repackaged for Kodi:

https://kodi.tv/addons/nexus/category/python-modules/

If you look at some of those, you'll see how the packaging needs to be done.
Thank you for the useful info
I think the point is that library module/package addons have their path added to sys path when your script adds them as dependencies in addon.xml.  At least on windows Kodi debug log logs the sys path so you should be able to verify.

scott s.
.
Hello I have another question
This is the repository: https://kodi.tv/addons/nexus/script.module.tmdbsimple/.
So, what's the difference between that and this link: https://kodi.wiki/view/Add-on:Tmdbsimple?"



I had manually installed the addon zip file in order to import it into my addon. How can I set it up for automatic installation when a user installs my addon?

Thank you
Edit about the first question only:
In the first link, the code is repackaged as a Kodi addon, but in the second link, it isn't..
It's the web site vs the wiki.

When you create your addon addon.xml file the elements:
xml:
<requires>
<import addon="script.module.tmdbsimple" version="2.2.0+matrix.1" />
</requires>
Kodi will first look for that addon within the user installed addons, with that minimum version number.  If not found it will attempt to install from Kodi official repo with no further user action needed.  If the user has set the option to update official addons from other repos, any other installed repos will also be checked to meet the requirement.  If the required addon isn't found in these locations, your addon won't install with toast that dependency wasn't met.

Note if you look at that addon on author's github, the addon.xml for it shows:
xml:
<extension point="xbmc.python.module" library="lib" />
When your addon is started in Kodi (either at start as a service or via runscript/runplugin) Kodi python system will add /addons/script.module.tmdbsimple/lib to the sys.path so you should be able to directly import any modules within "lib".  Looking at the source,
python:
from tmdbsimple import search
for example should work

scott s.
.
I think I've got it, thanks, It was packed and copied to the 'lib' folder because it's not in the official kodi repository
and it's not even in the user-installed addons
You can also notice that from this sentence:

Code:
<extension point="xbmc.python.module" library="lib" />