Home Download Buy Blog Forum Support

Depending on an extra module in a plugin

Depending on an extra module in a plugin

Postby asmodai on Mon Aug 09, 2010 5:47 am

So for the plugin I am developing, I have an extra module in the packages directory that I need to import.
In my plugin I use:

Code: Select all
from os.path import dirname
import sys

sys.path.insert(0, dirname(__file__))


to set the Python path for resolving. Is this the way to go about it in Sublime?

Edit: this trick seemed to work when I put my stuff in %APPDATA%\Sublime Text\Packages\User\, but now that I moved to making my plugin a real package and placed it all in %APPDATA%\Sublime Text\Packages\<NAME> I get an ImportError when the module tried to load one of its own files. Any hints/tips?
asmodai
 
Posts: 73
Joined: Mon Mar 01, 2010 10:42 am

Re: Depending on an extra module in a plugin

Postby asmodai on Tue Aug 10, 2010 1:49 pm

Ok, I am officially stumped on this one.

So my package resides in %APPDATA%\Sublime Text\Packages\MyPackage, in this I have my plugin code, plugin.py, and a subdirectory which is a full Python module. Now with or without any sys.path() tricks I simply keep on getting ImportErrors and others due to the module not being able to find specific files in MyPackage\module\subdir. Any hints/tips?
asmodai
 
Posts: 73
Joined: Mon Mar 01, 2010 10:42 am

Re: Depending on an extra module in a plugin

Postby guillermooo on Tue Aug 10, 2010 3:52 pm

sys.path will tell you which dirs python searches for modules. Remeber that there's the Sublime python interpreter and possibly your system's python. You only care about the first one.

How to add dirs to your python path

There are several ways to do this. Also, keep in mind that Sublime uses some tricks to work around paths containing unicode characters. As a result, all top-level folders under Packages are added to the python path, I think.

But I digress. Here's how you can add directories to your python path:

1) Append to sys.path from your module.
2) Use pth files (I don't know if these work under Sublime).
3) Add the path to your PYTHONPATH env var.
4) Create a package folder.

Go for (4), since most likely you are storing the code inside a Sublime package (terminology clash!--don't conflate Sublime package with Python package). To create a python package, simply add a __init__.py file into any directory in your python path. That way you tell the interpreter to search for modules within that folder too.

(3) should work by setting env vars from Sublime option files, but I've never tried myself yet.

Hope that helps!
guillermooo
 
Posts: 729
Joined: Thu Jul 23, 2009 9:06 am

Re: Depending on an extra module in a plugin

Postby asmodai on Wed Aug 11, 2010 9:22 am

I guess my posts were not clear enough.

Thanks for the explanation, but I am well versed in the Python module resolving. :)

I already have a package directory inside the %APPDATA%\Sublime Text\Packages for my package. The extra module (directory with subdirectories) lives in %APPDATA%\Sublime Text\Packages\MyPackage\. Even if I sys.path.insert() MyPackage and/or MyPackage\module I still get problems with the module not being able to import lower directories/files in its directory structure. Funnily enough, this seems to work as it should when the files are just residing in %APPDATA%\Sublime Text\Packages\User.
asmodai
 
Posts: 73
Joined: Mon Mar 01, 2010 10:42 am

Re: Depending on an extra module in a plugin

Postby gpfsmurf on Thu Aug 12, 2010 5:12 pm

I think you're doing something wrong, although I don't know what. You shouldn't need to alter sys.path().

As an example look at the WinSCP plugin

- I have it installed in "Packages\WinSCP"
- I have an external library installed in "Packages\WinSCP\grizzled\net\ftp\parse.py", with a __init__.py in each of the directories.
- In the plugin I use "from grizzled.net.ftp import parse"

It works, nothing special to do. Just copy the python module in your own package folder and do an import.
gpfsmurf
 
Posts: 211
Joined: Mon Jun 23, 2008 6:31 pm

Re: Depending on an extra module in a plugin

Postby asmodai on Fri Aug 13, 2010 1:42 pm

In my case I am using Pygments in my plugin.

So I have Packages\Pygmentize as my package with pygmentize.py and Pygmentize.package-menu in this directoy and the pygments main module directory as well.

In my pygmentize.py file I do:

Code: Select all
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import get_formatter_by_name

import sublime
import sublimeplugin

class DoPygmentize(sublimeplugin.TextCommand):
   def run(self, view, args):
      formatter = view.options().getString('pygmentsFormatter') or u'html'
      formatter = get_formatter_by_name(formatter)
...

When I run this from the plugin I get:

Code: Select all
Traceback (most recent call last):
  File ".\sublimeplugin.py", line 119, in execTextCommand
  File ".\pygmentize.py", line 20, in run
  File ".\pygments\formatters\__init__.py", line 51, in get_formatter_by_name
  File ".\pygments\formatters\html.py", line 341, in __init__
  File ".\pygments\formatter.py", line 67, in __init__
  File ".\pygments\formatter.py", line 22, in _lookup_style
  File ".\pygments\styles\__init__.py", line 55, in get_style_by_name
pygments.util.ClassNotFound: Could not find style module 'default', though it should be builtin.

This is despite every subdirectory having an __init__.py.

The part leading up to this in styles\__init__.py:

Code: Select all
    try:
        mod = __import__('pygments.styles.' + mod, None, None, [cls])
    except ImportError:
        raise ClassNotFound("Could not find style module %r" % mod +
                         (builtin and ", though it should be builtin") + ".")
asmodai
 
Posts: 73
Joined: Mon Mar 01, 2010 10:42 am

Re: Depending on an extra module in a plugin

Postby gpfsmurf on Fri Aug 13, 2010 4:56 pm

Did you try to manually import the module to see if it works?
Code: Select all
import pygments.styles.default
gpfsmurf
 
Posts: 211
Joined: Mon Jun 23, 2008 6:31 pm

Re: Depending on an extra module in a plugin

Postby jps on Sun Aug 15, 2010 1:24 am

I'm not quite sure what the cause of the issue is, but I suspect it'll go away if you include pygments as an egg, rather than have it there as a subdirectory: Sublime Text adds all .egg files to the python path, so none of the importing weirdness that has to be done should effect them.
jps
Site Admin
 
Posts: 3071
Joined: Wed Mar 19, 2008 12:33 pm

Re: Depending on an extra module in a plugin

Postby asmodai on Sun Aug 15, 2010 10:33 am

Yes, that also fails, but it shouldn't.

Within Sublime Text I get:

Code: Select all
>>> import pygments.styles.default
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named default

And from a normal Python 2.6 environment:

Code: Select all
Python 2.6.5 (r265:79063, Aug 12 2010, 11:13:06)
[GCC 4.2.1 20070719  [FreeBSD]] on freebsd8
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygments.styles.default
>>>
asmodai
 
Posts: 73
Joined: Mon Mar 01, 2010 10:42 am

Re: Depending on an extra module in a plugin

Postby asmodai on Sun Aug 15, 2010 11:38 am

jps wrote:I'm not quite sure what the cause of the issue is, but I suspect it'll go away if you include pygments as an egg, rather than have it there as a subdirectory: Sublime Text adds all .egg files to the python path, so none of the importing weirdness that has to be done should effect them.

Yeah, using an .egg works.
asmodai
 
Posts: 73
Joined: Mon Mar 01, 2010 10:42 am

Next

Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 7 guests