Best way to bundle/import third-party libs
#1
I am aware that you can make third-party libs (or your own libs) as a module so that other addons can use it. However, some libs are not commonly required by other addons, and you want to just bundle it with your own addon (without creating an external module). In the docs, it is stated that the own/third-party libs should be inside "PLUGIN/resources/lib". However, when I copy and paste a complex third-party lib (multiple files/sub-dirs/sub-modules) inside this directory, and try to import it with
Code:
from resources.lib.MODULE import CLASS
The import statement works fine, however, any further internal imports inside the third-party libs generate exceptions (due to non-existing module/class/method). This is probably due to the fact that they are not in the root point for the plugin. If I adjust the imports inside the libs, they work, but require a lot of work (and you cannot do that each time you update the lib, or add a new lib).
If, however, I place the third-party lib right in the root directory of the plugin itself "PLUGIN/" (right beside default.py), then there is no problem anymore (especially for internal imports for the complex third-party libs).
Is there a way to place third-part libs inside "PLUGIN/res[/code]ources/lib" (or any other dir for that matter) without missing the import for these libs?
Reply
#2
You need to make an __init__.py file inside the lib dir or other directories that you want to import from. It doesn't have to contain anything but you can add any initialization code if you wish.
Reply
#3
Already did that before posting, though no cookies for me Smile
If I don't do that, I won't be able to import in the first place. I have __init__.py in every single directory, and when I import in my default.py (in root of plugin), the import inside it is fine. The only problem is that the third-party libs are generating internal exceptions whenever they try to import their own sub-modules, and I couldn't figure why. One way to fix it is that I go inside each file of them and fix the imports accordingly, but I got tired of doing that. At the beginning I thought it was just as you suggested, but for some reason it didn't simply work. Currently, I am placing the libs in the root of the plugin, but I wish I could move them to "resources/lib"
Thanks for your help
Reply
#4
In your default.py (or wherever you're importing the lib), you need to manually set the imported lib to the name it's trying to import internally:
Code:
import sys
sys.modules['thirdPartylib'] = __import__('/resources/lib/some_file.py')

From then on, anything that tries to import thirdPartylib will get some_file.py
Reply
#5
No luck either. This is the exception I got
Code:
ImportError: Import by filename is not supported
Reply
#6
Sorry, did that wrong. Use the dot notation for the package, not the file name:
Code:
import sys
sys.modules['thirdPartylib'] = __import__('resources.lib.thirdPartylib')
Reply

Logout Mark Read Team Forum Stats Members Help
Best way to bundle/import third-party libs0