Kodi Community Forum
How Do I import my own class library - 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: How Do I import my own class library (/showthread.php?tid=132770)



How Do I import my own class library - _BJ1 - 2012-05-30

Hello folks,

I have written a special class file (e.g. called myOwnClasses.py) which should reside in the "resources/lib/" path of my addon. This file contains two classes (called class1 and class2) which I want import in my mainfile (default.py) in the root-directory of the addon.

I've tried:

Code:
import resources.lib.myOwnClasses as myOwnClass
from myOwnClass import class1, class2

but this fails. If I place myOwnClass.py in the same directory like default.py everything works, but I don't want that. How do I build the path to myOwnClass within Python?

_BJ1


RE: How Do I import my own class library - divingmule - 2012-05-30

You'll need to create a dummy file named __init__.py and place a copy in resources and lib directories.


RE: How Do I import my own class library - _BJ1 - 2012-05-31

(2012-05-30, 18:08)divingmule Wrote: You'll need to create a dummy file named __init__.py and place a copy in resources and lib directories.

That works! Thank you!




RE: How Do I import my own class library - Bstrdsmkr - 2012-05-31

FYI, if you end up needing it later on, any code you place inside __init__.py will be executed at the time you import the module


RE: How Do I import my own class library - _BJ1 - 2012-05-31

Yes, I know that __init__ is the constructor of a python-class, but I don't know that __init__.py also constructs the path to a module and initialize these own modules. Thank you very well for your information!

_BJ1


RE: How Do I import my own class library - Bstrdsmkr - 2012-06-01

It wasn't until later in my Python life that I learned this stuff lol

For other weird import situations such as importing a module by path or using only a str of it's file name, read up on __import__(). It comes in handy when you need a "plugin" structure for xbmc plugins (meaning allowing others to add functionality to your addon by adding a file to the folder)


RE: How Do I import my own class library - sphere - 2012-06-01

(2012-06-01, 00:03)Bstrdsmkr Wrote: For other weird import situations such as importing a module by path or using only a str of it's file name, read up on __import__(). It comes in handy when you need a "plugin" structure for xbmc plugins (meaning allowing others to add functionality to your addon by adding a file to the folder)

Btw. this is what im doing with the BigPictures Addon, a possible plugin loading structure could be seen here.

regards,
sphere