问题
I installed python 3.35 and I'm getting this error:
Traceback (most recent call last):
File "C:/Users/Augusto/PycharmProjects/Plot/Database.py", line 48, in <module>
import socket
File "C:\Python33\lib\socket.py", line 47, in <module>
import _socket
ImportError: DLL load failed: The specified module could not be found
I'm running it on Windows 7 64 bits. I tried both Python installations (Windows x86-64 MSI installer and Windows x86 MSI installer) but both present the same error. I removed the python 2.7 installation from the path in the chance it was causing the error, but no luck.
EDIT: os.system('listdlls %d' % os.getpid())
Base Size Path
0x000000001d000000 0xa000 C:\Python33\python.exe
0x0000000077b40000 0x1a9000 C:\Windows\SYSTEM32\ntdll.dll
0x00000000756f0000 0x3f000 C:\Windows\SYSTEM32\wow64.dll
0x0000000075690000 0x5c000 C:\Windows\SYSTEM32\wow64win.dll
0x0000000075680000 0x8000 C:\Windows\SYSTEM32\wow64cpu.dll
0x000000001d000000 0xa000 C:\Python33\python.exe
0x0000000077d20000 0x180000 C:\Windows\SysWOW64\ntdll.dll
0x0000000076b80000 0x110000 C:\Windows\syswow64\kernel32.dll
0x0000000076a20000 0x47000 C:\Windows\syswow64\KERNELBASE.dll
0x000000001e000000 0x294000 C:\Windows\SysWOW64\python33.dll
0x0000000075880000 0x100000 C:\Windows\syswow64\USER32.dll
0x0000000076500000 0x90000 C:\Windows\syswow64\GDI32.dll
0x0000000076b00000 0xa000 C:\Windows\syswow64\LPK.dll
0x0000000076760000 0x9d000 C:\Windows\syswow64\USP10.dll
0x0000000076800000 0xac000 C:\Windows\syswow64\msvcrt.dll
0x00000000768b0000 0xa0000 C:\Windows\syswow64\ADVAPI32.dll
0x0000000076590000 0x19000 C:\Windows\SysWOW64\sechost.dll
0x0000000076400000 0xf0000 C:\Windows\syswow64\RPCRT4.dll
0x0000000075750000 0x60000 C:\Windows\syswow64\SspiCli.dll
0x0000000075740000 0xc000 C:\Windows\syswow64\CRYPTBASE.dll
0x0000000076cd0000 0xc4a000 C:\Windows\syswow64\SHELL32.dll
0x0000000076700000 0x57000 C:\Windows\syswow64\SHLWAPI.dll
0x0000000062ba0000 0xbf000 C:\Windows\SysWOW64\MSVCR100.dll
0x0000000076050000 0x60000 C:\Windows\SysWOW64\IMM32.DLL
0x00000000757b0000 0xcc000 C:\Windows\syswow64\MSCTF.dll
0x00000000755d0000 0x35000 C:\Windows\SysWOW64\nvinit.dll
0x000000000f000000 0x6000 C:\Program Files (x86)\NVIDIA Corporation\CoProcManager\detoured.dll
0x00000000739f0000 0x4c000 C:\Program Files (x86)\NVIDIA Corporation\CoProcManager\nvd3d9wrap.dll
0x0000000075d80000 0x19d000 C:\Windows\syswow64\SETUPAPI.dll
0x0000000076c90000 0x27000 C:\Windows\syswow64\CFGMGR32.dll
0x0000000075990000 0x8f000 C:\Windows\syswow64\OLEAUT32.dll
0x00000000760b0000 0x15c000 C:\Windows\syswow64\ole32.dll
0x0000000075d60000 0x12000 C:\Windows\syswow64\DEVOBJ.dll
0x00000000739b0000 0x33000 C:\Program Files (x86)\NVIDIA Corporation\CoProcManager\nvdxgiwrap.dll
0x0000000074010000 0x16000 C:\Windows\SysWOW64\CRYPTSP.dll\rsaenh.dll
0x0000000066480000 0x15000 C:\Python33\DLLs\python3.dll
0x0000000065990000 0x41000 C:\Python33\lib\site-packages\PyQt4\QtSql.pyd
0x000000006a350000 0x34000 C:\Python33\lib\site-packages\PyQt4\QtSql4.dll
0x000000006a4f0000 0x273000 C:\Python33\lib\site-packages\PyQt4\QtCore4.dll
0x0000000076b10000 0x35000 C:\Windows\syswow64\WS2_32.dll
0x00000000763f0000 0x6000 C:\Windows\syswow64\NSI.dll
0x000000006f3d0000 0x69000 C:\Windows\SysWOW64\MSVCP100.dll
0x00000000685c0000 0x80d000 C:\Python33\lib\site-packages\PyQt4\QtGui4.dll
0x0000000076990000 0x7b000 C:\Windows\syswow64\COMDLG32.dll
0x0000000073270000 0x84000 C:\Windows\WinSxS_microsoft.windows.common-controls_6595b64144ccf1df_5.82.7601.18201_none_ec80f00e8593ece5\COMCTL32.dll
0x00000000718f0000 0x32000 C:\Windows\SysWOW64\WINMM.dll
0x0000000075610000 0x51000 C:\Windows\SysWOW64\WINSPOOL.DRV
0x0000000065970000 0x15000 C:\Python33\lib\site-packages\sip.pyd
0x0000000050d20000 0x19c000 C:\Python33\lib\site-packages\PyQt4\QtCore.pyd
0x0000000050770000 0x5ad000 C:\Python33\lib\site-packages\PyQt4\QtGui.pyd
0x0000000073bd0000 0x4c000 C:\Windows\SysWOW64pphelp.dll
回答1:
The output from listdlls.exe shows that there's no problem loading the dependency ws2_32.dll (Winsock), which would be surprising to say the least. So the problem is that it's trying to load the wrong _socket.pyd.
In the comments it was confirmed that loading _socket.pyd worked when an absolute path was passed to imp.load_dynamic
. It turns out that the system had the environment variable PYTHONPATH
set to include C:\Python27\DLLs
. The solution was to simply unset this environment variable.
I was thrown off by the error message. Normally such a case would raise an error that the shared library is compiled for a different Python version. However, _socket.pyd for Python 2.7 links with msvcr90.dll. This DLL isn't on the DLL search path. It's in a subdirectory of %windir%\WinSxS
. It's found by consulting the activation context of C:\Python27\python.exe
, which specifies the C runtime dependency in its embedded manifest:
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.VC90.CRT"
version="9.0.21022.8"
processorArchitecture="x86"
publicKeyToken="1fc8b3b9a1e18e3b">
</assemblyIdentity>
</dependentAssembly>
</dependency>
So when running under C:\Python33\python.exe
, trying to load the 2.7 _socket.pyd raises the following exception:
>>> imp.load_dynamic('_socket', r'C:\Python27\DLLs\_socket.pyd')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: DLL load failed: The specified module could not be found.
3.3 and 3.4 are built with Visual Studio 2010 instead of 2008, which switched back to installing msvcr100.dll on the normal search path. So for comparison here's the error I get when I try to import the 3.3 version of _socket.pyd
within 3.4:
>>> imp.load_dynamic('_socket', r'C:\Python33\DLLs\_socket.pyd')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: Module use of python33.dll conflicts with this version of Python.
来源:https://stackoverflow.com/questions/23130614/socket-module-import-error-on-fresh-python-3-3-5-install