How to include third party Python packages in Sublime Text 2 plugins

自闭症网瘾萝莉.ら 提交于 2019-11-28 04:31:54

You need to bundle full requests distribution with your Python package and then modify Python's sys.path (where it looks for modules) to point to a folder containing requests folder.

  • Download Requests library from a PyPi and extract it manually under your plugin folder

  • Before importing requests in your plugin, append the corrcet folder to sys.path to point a folder where it can found requests import

The (untested) code should look like something like this:

  import sys 
  import os

  # request-dists is the folder in our plugin
  sys.path.append(os.path.join(os.path.dirname(__file__), "requests-dist"))

  import requests

This also assumes that requests setup.py does not do any hacks when you install the module using easy_install or pip.

You also could import requests zip directly as Python supports importing from ZIP files, assuming requests is distributed in compatible way. Example (advanced):

https://github.com/miohtama/ztanesh/blob/master/zsh-scripts/python-lib/zipimporter.py

More about sys.path trick (2004)

http://www.johnny-lin.com/cdat_tips/tips_pylang/path.html

Mikko's answer is good, but I may have found a slightly easier way:

import MyAwesomePlugin.requests

"MyAwesomePlugin" being the name of your plugin, of course.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!