How Do I import my own class library

  Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post Reply
_BJ1 Online
Senior Member
Posts: 150
Joined: Apr 2010
Reputation: 0
Location: Germany
Post: #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
(This post was last modified: 2012-05-30 20:40 by _BJ1.)
find quote
divingmule Offline
Skilled Python Coder
Posts: 1,297
Joined: Oct 2008
Reputation: 61
Post: #2
You'll need to create a dummy file named __init__.py and place a copy in resources and lib directories.
find quote
_BJ1 Online
Senior Member
Posts: 150
Joined: Apr 2010
Reputation: 0
Location: Germany
Post: #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!
find quote
Bstrdsmkr Offline
Posting Freak
Posts: 802
Joined: Oct 2010
Reputation: 16
Post: #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
find quote
_BJ1 Online
Senior Member
Posts: 150
Joined: Apr 2010
Reputation: 0
Location: Germany
Post: #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
find quote
Bstrdsmkr Offline
Posting Freak
Posts: 802
Joined: Oct 2010
Reputation: 16
Post: #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)
find quote
sphere Offline
Team-XBMC Member
Posts: 1,179
Joined: Jul 2009
Reputation: 49
Location: Germany
Post: #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: IOoywq0.jpg]
find quote