使用 matplotlib 和 cx_Freeze 来自 ctypes 的 NotADirectoryError

NotADirectoryError from ctypes with matplotlib and cx_Freeze

我有一个使用 ctypes 和 matplotlib 的脚本。在 python 控制台上一切正常但冻结我从 ctypes 得到一个 NotADirectoryError 即使导入了 matplotlib。

debug.py:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import ctypes
import matplotlib    # produces NotADirectoryError in ctypes.WinDLL()

def main():
    dll = ctypes.WinDLL("CSSSM")

if __name__=="__main__":
    main()

setup.py:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from cx_Freeze import setup, Executable
import os, site

[python_dir, site_dir] = site.getsitepackages()
include_files = []
missing_dll = ["msvcr100.dll"]
for dll in missing_dll:
    include_files.append((os.path.join(python_dir, dll), dll))

buildOptions_exe = dict(
    compressed = False,
    includes = ["re"],
    include_files = include_files
    )

setup(
    name = "Debugger",
    author = "Name",
    version = "0.0.0",
    options = dict(build_exe = buildOptions_exe),
    executables = [Executable("debug.py"),]
)

冻结脚本的执行:

> build\exe.win32-3.3\debug.exe
Traceback (most recent call last):
  File "C:\Python\WinPython-32bit-3.3.2.3\python-3.3.2\lib\site-packages\cx_Free
ze\initscripts\Console3.py", line 27, in <module>
    exec(code, m.__dict__)
  File "debug.py", line 40, in <module>
  File "debug.py", line 36, in main
  File "C:\Python\WinPython-32bit-3.3.2.3\python-3.3.2\lib\ctypes\__init__.py",
line 353, in __init__
    self._handle = _dlopen(self._name, mode)
NotADirectoryError: [WinError 267] The directory name is invalid.

.

编辑: WinDbg,根据 eryksun 的评论,断点在 kernel32!LoadLibraryW

...
ModLoad: 65630000 65697000   O:\build\exe.win32-3.3\numpy.random.mtrand.pyd
ModLoad: 011e0000 01295000   O:\build\exe.win32-3.3\_hashlib.pyd
(304.898): Break instruction exception - code 80000003 (first chance)
eax=0023f400 ebx=65810815 ecx=00000000 edx=0023f39c esi=0023f2f8 edi=00f04b40
eip=750e3226 esp=0023f2f4 ebp=0023f2fc iopl=0         nv up ei pl nz na po nc
cs=0023  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00200202
*** ERROR: Symbol file could not be found.  Defaulted to export symbols for C:\Windows\syswow64\KERNELBASE.dll - 
KERNELBASE!DebugBreak+0x2:
750e3226 cc              int     3
0:000> bp kernel32!LoadLibraryW
*** WARNING: Unable to verify checksum for O:\build\exe.win32-3.3\_ctypes.pyd
*** ERROR: Symbol file could not be found.  Defaulted to export symbols for C:\Windows\syswow64\kernel32.dll - 
0:000> g
Breakpoint 0 hit
eax=02c6b940 ebx=1d1a8fc0 ecx=00000000 edx=0000004d esi=00f690a8 edi=00f6e1e8
eip=751548f3 esp=0023f2a4 ebp=0023f2b4 iopl=0         nv up ei pl nz na po nc
cs=0023  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00200202
kernel32!LoadLibraryW:
751548f3 8bff            mov     edi,edi
0:000> pt
eax=00000000 ebx=1d1a8fc0 ecx=7efdd000 edx=0000010b esi=00f690a8 edi=00f6e1e8
eip=75154905 esp=0023f2a4 ebp=0023f2b4 iopl=0         nv up ei pl zr na pe nc
cs=0023  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00200246
kernel32!LoadLibraryW+0x12:
75154905 c20400          ret     4
0:000> r @eax
eax=00000000
0:000> dt _TEB @$teb LastErrorValue
_ctypes!_TEB
   +0x034 LastErrorValue : 0x10b
0:000> g
eax=00000000 ebx=00000001 ecx=00000000 edx=00000000 esi=00000002 edi=003ef2d0
eip=77381f46 esp=0371fcc0 ebp=0371fe20 iopl=0         nv up ei pl zr na pe nc
cs=0023  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00000246
ntdll!NtWaitForWorkViaWorkerFactory+0x12:
77381f46 83c404          add     esp,4

.

编辑 2: WinDbg,根据 eryksun 的评论,断点在 ntdll!LdrLoadDll

...
ModLoad: 63570000 635d7000   O:\build\exe.win32-3.3\numpy.random.mtrand.pyd
ModLoad: 02170000 02225000   O:\build\exe.win32-3.3\_hashlib.pyd
(8e4.161c): Break instruction exception - code 80000003 (first chance)
eax=006af9b0 ebx=636e0815 ecx=00000000 edx=006af94c esi=006af8a8 edi=02754b40
eip=75f73226 esp=006af8a4 ebp=006af8ac iopl=0         nv up ei pl nz na po nc
cs=0023  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00200202
*** ERROR: Symbol file could not be found.  Defaulted to export symbols for C:\Windows\syswow64\KERNELBASE.dll - 
KERNELBASE!DebugBreak+0x2:
75f73226 cc              int     3
0:000> bp ntdll!LdrLoadDll
*** WARNING: Unable to verify checksum for O:\build\exe.win32-3.3\_ctypes.pyd
0:000> g
Breakpoint 0 hit
eax=006af850 ebx=00000000 ecx=75fa182c edx=00000001 esi=00000000 edi=773f7da3
eip=773fc4dd esp=006af810 ebp=006af848 iopl=0         nv up ei pl zr na pe nc
cs=0023  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00200246
ntdll!LdrLoadDll:
773fc4dd 8bff            mov     edi,edi
0:000> pt
eax=c0000103 ebx=00000000 ecx=774243d3 edx=00000000 esi=00000000 edi=773f7da3
eip=773fc596 esp=006af810 ebp=006af848 iopl=0         nv up ei pl zr na pe nc
cs=0023  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00200246
ntdll!LdrLoadDll+0xb9:
773fc596 c21000          ret     10h
0:000> r @eax
eax=c0000103
0:000> dt _TEB @$teb
*** ERROR: Symbol file could not be found.  Defaulted to export symbols for C:\Windows\syswow64\kernel32.dll - 
_ctypes!_TEB
   +0x000 NtTib            : _NT_TIB
   +0x01c EnvironmentPointer : (null) 
   +0x020 ClientId         : _CLIENT_ID
   +0x028 ActiveRpcHandle  : (null) 
   +0x02c ThreadLocalStoragePointer : 0x7efdd02c Void
   +0x030 ProcessEnvironmentBlock : 0x7efde000 _PEB
   +0x034 LastErrorValue   : 0
   +0x038 CountOfOwnedCriticalSections : 0
   +0x03c CsrClientThread  : (null) 
   +0x040 Win32ThreadInfo  : (null) 
   +0x044 User32Reserved   : [26] 0
   +0x0ac UserReserved     : [5] 0
   +0x0c0 WOW32Reserved    : 0x74882320 Void
   +0x0c4 CurrentLocale    : 0x407
   +0x0c8 FpSoftwareStatusRegister : 0
   +0x0cc SystemReserved1  : [54] (null) 
   +0x1a4 ExceptionCode    : 0n0
   +0x1a8 ActivationContextStack : _ACTIVATION_CONTEXT_STACK
   +0x1bc SpareBytes1      : [24]  ""
   +0x1d4 GdiTebBatch      : _GDI_TEB_BATCH
   +0x6b4 RealClientId     : _CLIENT_ID
   +0x6bc GdiCachedProcessHandle : (null) 
   +0x6c0 GdiClientPID     : 0
   +0x6c4 GdiClientTID     : 0
   +0x6c8 GdiThreadLocalInfo : (null) 
   +0x6cc Win32ClientInfo  : [62] 0
   +0x7c4 glDispatchTable  : [233] (null) 
   +0xb68 glReserved1      : [29] 0
   +0xbdc glReserved2      : (null) 
   +0xbe0 glSectionInfo    : (null) 
   +0xbe4 glSection        : (null) 
   +0xbe8 glTable          : (null) 
   +0xbec glCurrentRC      : (null) 
   +0xbf0 glContext        : (null) 
   +0xbf4 LastStatusValue  : 0xc0000103
   +0xbf8 StaticUnicodeString : _UNICODE_STRING ""
   +0xc00 StaticUnicodeBuffer : [261]  ""
   +0xe0c DeallocationStack : 0x005b0000 Void
   +0xe10 TlsSlots         : [64] (null) 
   +0xf10 TlsLinks         : _LIST_ENTRY [ 0x0 - 0x0 ]
   +0xf18 Vdm              : (null) 
   +0xf1c ReservedForNtRpc : 0x0095c7a8 Void
   +0xf20 DbgSsReserved    : [2] (null) 
   +0xf28 HardErrorMode    : 0
   +0xf2c Instrumentation  : [16] (null) 
   +0xf6c WinSockData      : (null) 
   +0xf70 GdiBatchCount    : 0x7efdb000
   +0xf74 InDbgPrint       : 0 ''
   +0xf75 FreeStackOnTermination : 0 ''
   +0xf76 HasFiberData     : 0 ''
   +0xf77 IdealProcessor   : 0 ''
   +0xf78 Spare3           : 0
   +0xf7c ReservedForPerf  : (null) 
   +0xf80 ReservedForOle   : (null) 
   +0xf84 WaitingOnLoaderLock : 0
   +0xf88 Wx86Thread       : _Wx86ThreadState
   +0xf94 TlsExpansionSlots : (null) 
   +0xf98 ImpersonationLocale : 0
   +0xf9c IsImpersonating  : 0
   +0xfa0 NlsCache         : (null) 
   +0xfa4 pShimData        : (null) 
   +0xfa8 HeapVirtualAffinity : 0
   +0xfac CurrentTransactionHandle : (null) 
   +0xfb0 ActiveFrame      : (null) 
   +0xfb4 FlsData          : 0x009558f8 Void

导入的 matplotlib(尤其是它导入的 numpy)在冻结脚本中将 OSError: The specified module could not be found 从 ctypes 更改为 NotADirectoryError: [WinError 267] The directory name is invalid

就是这样。如果安装了 DLL,一切正常。如果未导入 matplotlib,您也会在冻结脚本中得到一个 OSError: The specified module could not be found。但是如果导入了 matplotlib 并且脚本被冻结,那么 OSError: The specified module could not be found 就会变成奇怪的 NotADirectoryError: [WinError 267] The directory name is invalid.