1

I installed SublimeREPL in Sublime Text 3, and it's working great. However, whenever I try to import a module that uses _socket such as urllib2 and urllib, it gives me an ImportError. I ran os.path to verify that the path was correct. It also works perfectly fine from the python command line, just not in SublimeREPL.

>>> import urllib2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "E:\Python27\lib\urllib2.py", line 94, in <module>
    import httplib
  File "E:\Python27\lib\httplib.py", line 71, in <module>
    import socket
  File "E:\Python27\lib\socket.py", line 47, in <module>
    import _socket
ImportError: DLL load failed: %1 is not a valid Win32 application.
jstein123
  • 454
  • 2
  • 11

2 Answers2

2

You're almost certainly mixing Python installations. That is, you've got SublimeREPL using a 64-bit Python, but you've also got a 32-bit Python on the same machine, and your PYTHONPATH is configured to point at the 32-bit Python's library instead of/ahead of its own.

Or, worse, you installed both Pythons to the same directory, and you have a single installation which is part 64-bit and part 32-bit. (If that's the case, you'll get the same error using Python from the command line.)

Mixing native and Cygwin Pythons, CPython and IronPython, or occasionally even two builds compiled with different flags, or two different X.Y versions, can also cause this, but 32-bit and 64-bit are the most common reason.

The reason urllib2 itself loads is that Python 2.7 source is Python 2.7 source code; it doesn't matter what build it comes from. But C extensions compiled into DLLs are compiled against a specific Python interpreter, and only work on that interpreter. If the Windows DLL loader hadn't refused to let Python get any further, you'd just get a different error a moment later.

While we're at it, Python improved the error handling for this case somewhere around 3.3, making it a little easier to tell what's going on, but of course if you stick with 2.7 you don't get new features.


From inside Python, the quickest way to tell if you're in a 32-bit or 64-bit interpreter is sys.getmaxsize. If it's about 2 or 4 billion, you're 32-bit; if it's about 9 or 18 too-many-digits-to-count-illion, you're 64-bit.

To find out whether a DLL is 32-bit or 64-bit is apparently a lot harder on Windows than on any other platform in the universe. See this question or this one for details.

Community
  • 1
  • 1
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • I used `sys.maxsize` in the python command line and SublimeREPL and they both gave 2 billion. – jstein123 Aug 03 '14 at 18:50
  • @nagasgura: By "in the python command line", you mean you ran `E:\Python27\bin\Python.exe", right? Because if you have two different Python installations, and you just ran `python`, you could easily have gotten the one SublimeREPL is using, and not the one in `E:\Python27`. (If you're not sure how to answer that, also print out `sys.executable`.) – abarnert Aug 03 '14 at 22:44
  • yes, sys.executable prints the same path (E:\\Python27\\python.exe) in both pyscripter (which works) and SublimeREPL (which doesn't) – jstein123 Aug 04 '14 at 07:01
  • @nagasgura: OK, can you figure out (from the answers to the two links I gave) how to tell if the `_socket` module's DLL (usually `_socket.pyd`) is 32-bit or 64-bit? If it's 32-bit, that rules out the most obvious problem. – abarnert Aug 04 '14 at 07:48
  • Yep, it's a 32 bit DLL. – jstein123 Aug 05 '14 at 06:42
  • @nagasgura: OK, and you don't have IronPython, or Cygwin, or anything else installed that might be conflicting with your native 32-bit CPython, right? – abarnert Aug 05 '14 at 22:01
  • Correct. Thank you for all your help by the way. – jstein123 Aug 06 '14 at 04:07
0

I ran into the same problem. I suspected it had to do with using SublimeText 3 64bit and using a 32bit Python 3 for SublimeREPL.

I uninstalled Python 3 32bit, and installed 64bit Python 3 but this presented a new error about a version mismatch. I had installed Python 3.4 and my current version of SublimeText bundles Python 3.3.

So, I installed Python 3.3 64bit to match the bundled python of SublimeText 3 (Build 3083) and it finally worked. This is from within a REPL tab in Sublime Text:

Python 3.3.5 (v3.3.5:62cf4e77f785, Mar  9 2014, 10:35:05) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib.request
>>> 
Andre Miller
  • 15,255
  • 6
  • 55
  • 53