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

I'm writing a sublime text 2 plugin that uses a module SEAPI.py which in itself imports the requests module.

Since sublime text 2 uses it's own embedded python interpreter, it doesn't see the requests module installed in my ubuntu machine (I get the following error: ImportError: No module named requests).

Best solution I could find so far was to copy the 'requests' module (the whole directory of files) from /usr/lib/python2.7/dist-packages/requests into my plugin directory in the sublime text packages dir.

But after that, it says that it can't find the 'urllib3' module.

Is there a better way to import the requests module so that I won't have to copy all the files into my plugin directory ?

The current code I'm using is as follows:

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

...

Edit:

The selected answer is correct and fixes my main question, but a different problem exists with using the current version of 'Requests' with the embedded sublime text 2 interpreter. ST2's python is missing various modules which exist in regular 2.7 python (such as 'fileio').

I've solved it with using the 'Requests' module from here:

https://github.com/bgreenlee/sublime-github

And I had to edit the 'urllib3/response.py' file to this:

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

解决方案

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):

More about sys.path trick (2004)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值