如何在Sublime Text 2插件中包含第三方Python包
我正在编写一个使用模块
SEAPI.py的sublime text 2插件,该模块本身导入了
requests module。
由于sublime text 2使用它自己的嵌入式python解释器,它没有看到我的ubuntu机器中安装的请求模块(我收到以下错误:ImportError:没有名为requests的模块)。 到目前为止我能找到的最佳解决方案是将/usr/lib/python2.7/dist-packages/requests中的’requests’模块(整个文件目录)复制到sublime文本包dir中的plugin目录中。 有没有更好的方法导入请求模块,以便我不必将所有文件复制到我的插件目录? 我正在使用的当前代码如下: MyPlugin.py import sublime import sublime_plugin import SEAPI ... SEAPI.py import requests try: import simplejson as json except: import json from time import time,sleep ... 编辑: 我从这里使用’请求’模块解决了它: 我必须编辑’urllib3 / response.py’文件到这个: try: from cStringIO import StringIO as BytesIO except ImportError: pass # _fileio doesn't seem to exist in ST's python in Linux,but we don't need it 解决方法
您需要将完整的请求分发与Python包捆绑在一起,然后修改Python的sys.path(它查找模块)以指向包含requests文件夹的文件夹。
>从PyPi下载请求库并在插件文件夹下手动提取 (未经测试的)代码应如下所示: 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_install或pip安装模块时,请求setup.py不会发生任何黑客攻击。 假设请求以兼容的方式分发,您还可以直接导入请求zip,因为Python支持从ZIP文件导入。示例(高级): https://github.com/miohtama/ztanesh/blob/master/zsh-scripts/python-lib/zipimporter.py 有关sys.path技巧的更多信息(2004) http://www.johnny-lin.com/cdat_tips/tips_pylang/path.html (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |