py2exe MemoryLoadLibrary 加载 _ssl.pyd 失败,Win7<->Win10

sno*_*odo 2 python ssl py2exe python-2.7

介绍

我有一个使用 SSL 的脚本,并使用 py2exe 构建(bundle_files=1,将所有内容打包到 *.exe 中)

现在我遇到了这个问题

在 Win7 上运行 py2exe 会创建一个 *.exe,它将在 Win7 和 Win10 中运行
在 Win10 上运行 py2exe 将创建一个 *.exe,它将在 Win10 中运行,但在 Win7 中会产生此错误:

ImportError: MemoryLoadLibrary failed loading _ssl.pyd
Run Code Online (Sandbox Code Playgroud)

解决方法

将bundle_files设置为3(不打包)将生成一个*.exe,即使它是在Win10上构建的,它也可以在Win7中正常工作。

我尝试了一些 py2exe 选项,在更改 bundle_files 时突然它起作用了。但我不明白为什么。

我的设置

  • 蟒蛇 32 位 2.7.11
  • ssl.OPENSSL_VERSION => 'OpenSSL 1.0.2d 2015 年 7 月 9 日'
  • py2exe 0.6.9

两台机器上都一样(win7和win10)。

这是一个重现它的最小演示:

演示.py

import ssl
print "import done"
Run Code Online (Sandbox Code Playgroud)


可以使用这个exebuilder.py来构建它

from distutils.core import setup
import py2exe
import sys

sys.argv.append("py2exe") # run py2exe (instead of supplying the command line argument)

# exclude some DLLs
dll_excludes = [
    # win9x leftovers
    "w9xpopen.exe",
    # don't import these - otherwise win7 created *.exe won't work in winXP
    # http://stackoverflow.com/questions/1979486/py2exe-win32api-pyc-importerror-dll-load-failed
    "mswsock.dll",
    "powrprof.dll"
]
sys.argv.append("--dll-excludes=%s" % ",".join(dll_excludes))

app_name = "win10ssl"
params = {
    'zipfile': None, # pack everything into the *.exe
    'options': {
        "py2exe": {
            "compressed": 1,
            "optimize": 2,
            # bundle_files
            # 1 = EVERYTHING packed into the *.exe
            # 2 = everything except for the pythonXX.dll
            # 3 = don't pack
            "bundle_files": 3
        }
    },
    'version': "0.0.1.0",
    'description': "demo to show MemoryLoadLibrary error",
    'name': app_name,
    'console': [{
            "script": "demo.py",
            "dest_base": app_name
        }
    ]
}

setup(**params)
Run Code Online (Sandbox Code Playgroud)

小智 5

将“crypt32.dll”和“mpr.dll”添加到您的 dll_excludes 中。这些由 _ssl.pyd 在较新版本的 Python(例如 2.7.11)中加载。但这些库依赖于 Windows 系统库和操作系统版本,因此不应将它们与您的项目一起打包和分发。Win7“crypt32.dll”可能适用于Win10,但Win10“crypt32.dll”很可能不适用于Win7。