且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Sublime Text 2插件中包含第三方Python软件包

更新时间:2023-01-09 09:09:00

您需要将完整的请求分发与Python包捆绑在一起,然后修改Python的sys.path(在其中查找模块)指向包含文件夹.

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.

  • 从PyPi下载请求库并手动将其解压缩到插件文件夹下

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

之前,在您的插件中导入请求之前,请将corrcet文件夹附加到sys.path中,以指向可以找到请求导入的文件夹

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

这还假设当您使用easy_installpip安装模块时,requests setup.py不会受到任何黑客攻击.

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

您还可以直接导入requests zip,因为Python支持从ZIP文件导入(假设请求以兼容的方式分发).示例(高级):

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

有关sys.path技巧(2004)的更多信息

More about sys.path trick (2004)

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