简体   繁体   中英

Install package for Sublime Text build-in Python?

I need to import a third-party package in my Sublime plugin. It seems Sublime uses non-system Python interpreter, so can't import packages, previously installed by sudo pip install ... globally for system Python.

Is there a workaround to install package for build-in Sublime Python? Or make Sublime use system Python for running plugins? However, it second case will Python packages ( sublime, sublime_plugin ) be available for import?

My primary version is Sublime Text 3. OSX 10.10

Thank you.

Please see my answer here for full details, but essentially the answer is to put the package(s) needed in your plugin directory (make sure their license(s) allow for redistribution in this manner) as a separate folder, then use the following model:

try: #ST3
    from .foobar import mymodule
    import .baz
except ImportError: #ST2
    from foobar import mymodule
    import baz

for importing the modules. The main issues with your answer are that A) it is Mac-specific, B) it is ST2-specific, and C) it's not portable - you can't distribute your plugin using this method.

It would be easiest to only use pure Python modules that work with both 2.6 and 3.3 (if you want to target both ST2 and ST3). If you use a compiled module ( lxml , numpy , whatever), you'll need to have versions compiled individually for 2.6 and 3.3 (again, if you're supporting both editor versions), and within that compiled for OS X, Linux, and Windows. Finally, for Linux and Windows you'll need both 32- and 64-bit versions (OS X is 64-bit only). The only package I'm aware of that does this is the PyV8 node/js engine for Emmet and I think maybe 1 or 2 others. As you can see, it'd be a real pain in the neck to support and upgrade.

One solution I've found is to switch to Sublime Text 2. It uses system Python 2.6, so it's possible to install plugin for it's Python:

>>> import os
>>> print(os.__file__)
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.pyc

So, I've just ran

sudo /System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6 setup.py install

in my package dir and now able to import package from Sublime2 console/plugins.

Sublime Text 3 solution is still missing.

Relative import doesn't work if the 3rd-party package itself imports another module and doesn't use relative imports (many of packages don't use relative imports, PEP8 ).

My solution is put all third-party packages and their dependencies inside your plugin subfolder. And that subfolder add to sys.path (it looks like dirty hack but it works):

Assume plugin has a structure somewhat like this:

plugin Folder/
-- ext/
---- foo/ <-- this module require bar module
---- bar/ <-- so we include it too
---- baz/ <-- yet another 3rd-party python module for our plugin
-- plugin.py

so inside the plugin.py we can write something like that:

sys.path.append(os.path.join(os.path.dirname(__file__), "ext"))
import foo
from baz import foobaz

  • ST 2&3 compatible
  • cross-platform
  • portable

  • looks like we are going to depency-hell because other plugins may include different version of same python module or even completely different module with same name.

For third-party packages without dependencies the best way to remain explicit relative imports.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM