Search code examples
pythonsublimetext2package-control

ImportError when trying to install Package Control in Sublime Text 2


I tried installing Package Control in Sublime Text 2. I entered the following install code from the official website into the console:

import urllib2,os,hashlib; 
h = '7183a2d3e96f11eeadd761d777e62404' + 'e330c659d4bb41d3bdf022e94cab3cd0'; 
pf = 'Package Control.sublime-package'; 
ipp = sublime.installed_packages_path(); 
os.makedirs( ipp ) if not os.path.exists(ipp) else None; 
urllib2.install_opener( urllib2.build_opener( urllib2.ProxyHandler()) ); 
by = urllib2.urlopen( 'http://sublime.wbond.net/' + pf.replace(' ', '%20')).read(); 
dh = hashlib.sha256(by).hexdigest(); 
open( os.path.join( ipp, pf), 'wb' ).write(by) if dh == h else None; 
print('Error validating download (got %s instead of %s), please try manual install' % (dh, h) if dh != h else 'Please restart Sublime Text to finish installation')`

However, the only result I got was:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named 'urllib2'

I don't understand why urllib2 isn't there. I checked which version of Python Sublime is using and it's 3.3.3.

>>> import sys
>>> print(sys.version)
3.3.3 (default, Dec 19 2013, 14:22:24) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.24)]

Any ideas?


Solution

  • The problem is that you're using Python 3, and you're following instructions for Python 2.

    Among the many differences between the two languages is that Python 3 reorganized urllib2 and a bunch of related modules into the urllib package. In particular, the urlopen function, the ProxyHandler class, etc. are now in urllib.request instead of urlopen.

    However, you'd probably do better looking for Python 3 instructions than trying to hack up the Python 2 instructions to work.

    Especially since, if there aren't any Python 3 instructions, there's a decent possibility that the program doesn't work with Python 3 and will just end up leaving unusable garbage in your site-packages or Sublime plugins or something.

    Even more so because usually if you're using Python 3 with Sublime Text, you're actually using Sublime Text 3 beta, which has some changes of its own from Sublime Text 2.