Home Download Buy Blog Forum Support

[ST3] Importing 3rd party python libraries in plugin

[ST3] Importing 3rd party python libraries in plugin

Postby PaulK on Tue Jul 02, 2013 7:44 pm

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 "<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 "C:\Users\kuykepau\AppData\Roaming\Sublime Text 3\Packages\RDFTranslator\RDFTranslator.py", line 10, in <module>
import rdflib
ImportError: No module named 'rdflib'

Note that the first line is my sys.path.

Thanks
PaulK
 
Posts: 4
Joined: Tue Jul 02, 2013 3:19 pm

Re: [ST3] Importing 3rd party python libraries in plugin

Postby miped on Mon Jul 08, 2013 7:09 pm

Hi Paul,

I've found with my own plugin which has a structure somewhat like this:
Code: Select all
Plugin Dir/
    sgit/ <-- submodule containing commands
    Plugin.py <-- "bootstrap" file

I've had to do relative imports in the bootstrap file, like so:
Code: Select all
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:
Code: Select all
RDFImport/
    rdflib/ <-- rdflib 4.0.1
    rdfplugin.py <-- plugin file

And in rdfplugin.py i added this code:
Code: Select all
import sys
import os

sys.path.append(os.path.dirname(__file__))
from .rdflib import Namespace

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:
Code: Select all
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:
Code: Select all
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.
SublimeGit: Git integration for Sublime Text 2 and 3
https://sublimegit.net
miped
 
Posts: 16
Joined: Mon Jul 08, 2013 6:30 pm

Re: [ST3] Importing 3rd party python libraries in plugin

Postby PaulK on Wed Jul 10, 2013 9:50 pm

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!
PaulK
 
Posts: 4
Joined: Tue Jul 02, 2013 3:19 pm


Return to Plugin Development

Who is online

Users browsing this forum: Exabot [Bot], Yahoo [Bot] and 7 guests