How Do I import my own class library
#1
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
Reply
#2
You'll need to create a dummy file named __init__.py and place a copy in resources and lib directories.
Reply
#3
(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!

Reply
#4
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
Reply
#5
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
Reply
#6
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)
Reply
#7
(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
My GitHub. My Add-ons:
Image
Reply

Logout Mark Read Team Forum Stats Members Help
How Do I import my own class library0