Kodi Community Forum
Unable to import external script to addon default.py within plugin.video.addon folder - Printable Version

+- Kodi Community Forum (https://forum.kodi.tv)
+-- Forum: Development (https://forum.kodi.tv/forumdisplay.php?fid=32)
+--- Forum: Add-ons (https://forum.kodi.tv/forumdisplay.php?fid=26)
+--- Thread: Unable to import external script to addon default.py within plugin.video.addon folder (/showthread.php?tid=355160)



Unable to import external script to addon default.py within plugin.video.addon folder - sshah93 - 2020-06-13

I am re-learning how to make Kodi addons. i have the following sample directory structure:


plugin.video.test_addon
    resources
        --> addon_utils.py
    addon.xml
    default.py
    globals.py

On the  default.py when ever i try either of the following commands, I get the assoicated error messages:
  1. from globals import * - ERROR: ImportError: No module named globals
  2. from resources.addon_utils import * - ERROR: ImportError: No module named resources.addon_utils 


As you can see, my question is how do I import scripts within the my plugin.video.addon directory, as currently it is causes the above ImportErrors?

Many Thanks.


RE: Unable to import external script to addon default.py within plugin.video.addon folder - pkscout - 2020-06-14

The currently shipping version of Kodi is still using a Python 2.7 interpreter (the upcoming Matrix release switches to Python3).  Under Python 2.7 You must have an __init__.py file (even an empty one will work) in every directory you want treated as a module.  I'd suggest moving globals.py to resources and then putting an __init__.py file in the resources folder.  Then you should be able to do:

python:

from resources.global import *
from resources.addon_utils import *



RE: Unable to import external script to addon default.py within plugin.video.addon folder - Roman_V_M - 2020-06-14

globals is a reserved name in Python.


RE: Unable to import external script to addon default.py within plugin.video.addon folder - sshah93 - 2020-06-15

(2020-06-14, 06:48)pkscout Wrote: The currently shipping version of Kodi is still using a Python 2.7 interpreter (the upcoming Matrix release switches to Python3).  Under Python 2.7 You must have an __init__.py file (even an empty one will work) in every directory you want treated as a module.  I'd suggest moving globals.py to resources and then putting an __init__.py file in the resources folder.  Then you should be able to do:

python:

from resources.global import *
from resources.addon_utils import *
Hi @pkscout,

Thank you very much, your solution to make a blank __init__.py file within the resources folder worked well, as I am now succefully able to import scripts from the resources directory.

Thank you very much for your help Smile


RE: Unable to import external script to addon default.py within plugin.video.addon folder - sshah93 - 2020-06-15

(2020-06-14, 07:21)Roman_V_M Wrote: globals is a reserved name in Python.
Hi @Roman_V_M ,

Thank you for this, I am well aware globals is a reserved name, silly me for using the name as an example instead of something like 'config.py'.

Anyways, the main intent of including the globals.py example was to show that I was having trouble importing a script from either the main 'plugin.video.addon' directory and/or the 'resources' directory aswell.

Really appreciate the advice and the time you gave to read and respond to my post.

Thank you Smile .


RE: Unable to import external script to addon default.py within plugin.video.addon folder - Roman_V_M - 2020-06-16

There shouldn't be any problems with importing modules located at the same level. As for modules in subfolders, you need either add __init__.py file to convert a subfolder to a Python package or add this subfolder explicitly to sys.path list. BTW, what are your sys.path contents when you are doing the import?


RE: Unable to import external script to addon default.py within plugin.video.addon folder - sshah93 - 2020-07-11

(2020-06-16, 12:37)Roman_V_M Wrote: There shouldn't be any problems with importing modules located at the same level. As for modules in subfolders, you need either add __init__.py file to convert a subfolder to a Python package or add this subfolder explicitly to sys.path list. BTW, what are your sys.path contents when you are doing the import?
Hi @Roman_V_M, sorry for the late reply, having just tested it again I realised that what you said is correct, I am able to import from the same folder any script fine, I am not sue why I thought couldnt at the time. Thank you for your help and sorry for troubling you.