Sublime Forum

[Question][Solved] xgoogle in Sublime Text 3

#1

1. Summary

I don’t understand, how I can use xgoogle for Sublime Text plugins creating.

2. Environment

  • Windows 10 Enterprise LTSB EN,
  • Python 3.6.4,
  • Sublime Text Build 3143.

3. Steps to reproduce

I successful install xgoogle for Python 3:

  • hub clone mycognitive/xgoogle,
  • cd xgoogle,
  • python setup.py install.

Then I try to get first Google SERP link by query Sublime Text 3:

Example, how do it.

4. Expected behavior

Example in SublimeREPL interpreter:

Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from xgoogle.search import GoogleSearch
>>> br = GoogleSearch("Sublime Text 3")
>>> br.results_per_page = 1
>>> results = br.get_results()
>>> for res in results:
… 	print(res.url.encode('utf8'))
… 
b'https://www.sublimetext.com/3'
>>> 

Good.

5. Actual behavior

Same actions in Sublime Text plugin:

import sublime_plugin

from xgoogle.search import GoogleSearch


class SashaExampleSsl(sublime_plugin.TextCommand):

    def run(self, edit):

        br = GoogleSearch("Sublime Text 3")
        br.results_per_page = 1
        results = br.get_results()
        for res in results:
            print(res.url.encode('utf8'))

[{
	"caption": "Sasha Example SSL",
	"command": "sasha_example_ssl"
}]

I run sasha_example_ssl command → I get stack trace in console:

command: sasha_example_ssl
Traceback (most recent call last):
  File "D:\Sublime Text 3 x64\sublime_plugin.py", line 818, in run_
    return self.run(edit)
  File "D:\Sublime Text 3 x64\Data\Packages\TestPlugin\example.py", line 16, in run
    br = GoogleSearch("Sublime Text 3")
  File "C:\Python36\lib\site-packages\xgoogle-1.4-py3.6.egg\xgoogle\search.py", line 109, in __init__
    self.browser = Browser(debug=debug)
  File "C:\Python36\lib\site-packages\xgoogle-1.4-py3.6.egg\xgoogle\browser.py", line 99, in __init__
    ssl._create_default_https_context = ssl._create_unverified_context
AttributeError: 'module' object has no attribute '_create_unverified_context'

I search error text in Google → I find:

My bad, I was not checking that you are using Python3.4. As far as I know, _create_unverified_context exists in Python3.6 but is not supported in Python3.4. You can check yourself by looking at Lib\ssl.py.

Is any method, how use xgoogle for Sublime Text 3 plugins?

Thanks.

0 Likes

#2

Sublime is using it own python environment, isolated from your installation, that’s why your import fails. So the xgoogle module needs to be inside your plugin (in a sub-directory).

0 Likes

#3

At the time I can’t add external modules, not compatible with Python 3.3.

See Global Python packages in Sublime Text plugin development.

Thanks.

1 Like