Sublime Forum

[ST3] Importing 3rd party python libraries in plugin

#1

Is there a simple way of importing 3rd party libraries into a ST3 plugin? I have looked for the I have placed the relevant libraries in my plugin directory but am receiving the following error:

‘C:\Program Files\Sublime Text 3’, ‘C:\Program Files\Sublime Text 3/python3.3.zip’, ‘C:\Users\kuykepau\AppData\Roaming\Sublime Text 3\Packages’, ‘C:\Users\kuykepau\AppData\Roaming\Sublime Text 3\Installed Packages\Emmet.sublime-package’, ‘C:\Users\kuykepau\AppData\Roaming\Sublime Text 3\Installed Packages\Emmet.sublime-package\emmet_completions’, ‘C:\Users\kuykepau\AppData\Roaming\Sublime Text 3\Installed Packages\Emmet.sublime-package\emmet’, ‘C:\Users\kuykepau\AppData\Roaming\Sublime Text 3\Installed Packages\PyV8’, ‘C:\Users\kuykepau\AppData\Roaming\Sublime Text 3\Installed Packages\PyV8\win64-p3’, ‘C:\Users\kuykepau\AppData\Roaming\Sublime Text 3\Installed Packages\PyV8\pyv8-win64-p3’, ‘C:\Users\kuykepau\AppData\Roaming\Sublime Text 3\Packages\RDFTranslator\rdflib’]
Traceback (most recent call last):
File “C:\Program Files\Sublime Text 3\sublime_plugin.py”, line 71, in reload_plugin
m = imp.reload(m)
File “X/imp.py”, line 252, in reload
File “”, line 586, in _check_name_wrapper
File “”, line 1023, in load_module
File “”, line 1004, in load_module
File “”, line 562, in module_for_loader_wrapper
File “”, line 869, in _load_module
File “”, line 313, in _call_with_frames_removed
File “C:\Users\kuykepau\AppData\Roaming\Sublime Text 3\Packages\RDFTranslator\RDFTranslator.py”, line 10, in
import rdflib
ImportError: No module named ‘rdflib’

Note that the first line is my sys.path.

Thanks

0 Likes

#2

Hi Paul,

I’ve found with my own plugin which has a structure somewhat like this:

Plugin Dir/ sgit/ <-- submodule containing commands Plugin.py <-- "bootstrap" file
I’ve had to do relative imports in the bootstrap file, like so:

from .sgit import SomeCommand

However if the module itself doesn’t use relative imports, this wont work. I made a quick test plugin with the following structure:

RDFImport/ rdflib/ <-- rdflib 4.0.1 rdfplugin.py <-- plugin file
And in rdfplugin.py i added this code:

[code]import sys
import os

sys.path.append(os.path.dirname(file))
from .rdflib import Namespace[/code]
What this does is append the directory of the rdfplugin.py file to the sys.path so that rdflib can import its submodules. It’s a little nasty, but it seems to do the trick import-wise. However, i get this problem when importing it:

reloading plugin RDFImport.rdfplugin Traceback (most recent call last): File "/Applications/Sublime Text 3.app/Contents/MacOS/sublime_plugin.py", line 73, in reload_plugin m = importlib.import_module(modulename) File "X/importlib/__init__.py", line 88, in import_module File "<frozen importlib._bootstrap>", line 1577, in _gcd_import File "<frozen importlib._bootstrap>", line 1558, in _find_and_load File "<frozen importlib._bootstrap>", line 1525, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 586, in _check_name_wrapper File "<frozen importlib._bootstrap>", line 1023, in load_module File "<frozen importlib._bootstrap>", line 1004, in load_module File "<frozen importlib._bootstrap>", line 562, in module_for_loader_wrapper File "<frozen importlib._bootstrap>", line 869, in _load_module File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed File "/Users/mp/Library/Application Support/Sublime Text 3/Packages/RDFImport/rdfplugin.py", line 8, in <module> from .rdflib import Namespace File "/Users/mp/Library/Application Support/Sublime Text 3/Packages/RDFImport/rdflib/__init__.py", line 108, in <module> from rdflib.term import ( File "/Users/mp/Library/Application Support/Sublime Text 3/Packages/RDFImport/rdflib/__init__.py", line 108, in <module> from rdflib.term import ( File "/Users/mp/Library/Application Support/Sublime Text 3/Packages/RDFImport/rdflib/term.py", line 1551 def __new__(cls, (subject, predicate, object), context): ^ SyntaxError: invalid syntax
So i guess if you can find a version of rdflib which doesn’t have this bug, you should be golden. Just remember that rdflib comes with it own set of dependencies, which you will also have to include. As well as their dependencies etc etc… It becomes a long list pretty quickly. For a basic install of rdflib it seems to be this list:

SPARQLWrapper==1.5.2 html5lib==1.0b2 isodate==0.4.9 pyparsing==1.5.7 rdflib==4.0.1 six==1.3.0
So you’d have to include all of those to get it working.

0 Likes

#3

Hi miped,

Thanks for the reply. I’ll check out your suggestions tonight when I get home. I knew there would be many things that would need to be included. Push come to shove, I can implement it using a web call to an online converter, but I’m really getting tired of sending lots of stuff over the wire when I could convert it locally.

Again, thanks!

0 Likes