Search code examples
pythonpluginsmodulepython-importxbmc

Importing a module in resources/ into an XBMC plugin


I'm working on an XBMC plugin which requires a few Python modules not available via a requires tag in addon.xml (they are not in the XBMC repository as far as I'm aware). The documentation for plugin development seems to indicate that you can do this by adding the module(s) to the resources/lib/ subdirectory of your plugin directory.

I've done this and when testing it in XBMC I get an import error trying to import this module because it cannot be found.

I've read the other question I found on SO regarding this topic entitled Importing a python module into XBMC for use within an addon but the proposed solution there, of adding the module directory to the path before importing, doesn't work for me either. I get the same import error.

In fact I don't think that answer is correct because os.getcwd() in XBMC does not return your plugin directory path when called from within your plugin; so concatenating the path it gives with /resources/lib as the answer suggests won't yield a valid path. I modified the example to use getAddonInfo to find the plugin path from an Addon object via the xbmcaddon module and added that to the path concatenated with /resources/lib but it still did not work.

Putting the modules into the root of the plugin directory also doesn't work. I haven't been able to find specific documentation about how to do this correctly beyond the initial tutorial saying that I should add it to the resources/lib subdirectory.

So does anyone know how to do this or have an example of this being done successfully in another XBMC plugin?


Solution

  • Figured out my mistake. I wasn't paying attention to the path I was adding. I was adding the addon profile directory to sys.path using getAddonInfo('profile') when I should have been using getAddonInfo('path')

    For future reference, if you want to add a subdirectory of your addon to the path, this is what I did:

    import xbmcaddon
    import os
    ...
    
    my_addon = xbmcaddon.Addon('plugin.video.my_plugin')
    addon_dir = xbmc.translatePath( my_addon.getAddonInfo('path') )
    
    sys.path.append(os.path.join( addon_dir, 'resources', 'lib' ) )
    
    import <whatever modules you want>
    

    I guess that's yet another lesson in paying close attention to the content of an error message.