我最近从 Python 2.5 更新到 2.7(在麻烦期间尝试了 2.6),虽然从命令行或 Django runserver 中一切正常,但 mod_wsgi 无法加载任何包含使用 MSVC 构建的 DLL (pyd) 的模块。
例如,如果我构建自己的 pycrypto 或 lxml 版本,那么我只会从 mod_wsgi 收到以下错误:
ImportError at /
DLL load failed: The specified module could not be found.
即使官方的 PIL 二进制文件也无法在 mod_wsgi 中导入 _imaging C 模块,但这可能是另一个问题。
但是,如果我使用像这样的地方用 MinGW 构建的 pycrypto 版本http://www.voidspace.org.uk/python/modules.shtml#pycrypto http://www.voidspace.org.uk/python/modules.shtml#pycrypto那么即使在 mod_wsgi 中它也能正常导入。但我觉得这个解决方案并不令人满意,因为我更新 Python 的全部原因是为了避免需要寻找预构建的二进制文件,而且我无法自己构建它们,因为 MinGW 对我来说超过 50% 的时间都会失败。
编辑2:
我在 Python27/Lib/distutils/msvc9compiler.py 的第 680-705 行注意到这一点:
try:
# Remove references to the Visual C runtime, so they will
# fall through to the Visual C dependency of Python.exe.
# This way, when installed for a restricted user (e.g.
# runtimes are not in WinSxS folder, but in Python's own
# folder), the runtimes do not need to be in every folder
# with .pyd's.
manifest_f = open(manifest_file)
try:
manifest_buf = manifest_f.read()
finally:
manifest_f.close()
pattern = re.compile(
r"""<assemblyIdentity.*?name=("|')Microsoft\."""\
r"""VC\d{2}\.CRT("|').*?(/>|</assemblyIdentity>)""",
re.DOTALL)
manifest_buf = re.sub(pattern, "", manifest_buf)
pattern = "<dependentAssembly>\s*</dependentAssembly>"
manifest_buf = re.sub(pattern, "", manifest_buf)
manifest_f = open(manifest_file, 'w')
try:
manifest_f.write(manifest_buf)
finally:
manifest_f.close()
except IOError:
pass
这可能解释了为什么一切都可以在命令行中运行,但不能在 mod_wsgi 中运行。注释掉所有这些似乎可以解决问题,但感觉不是正确的解决方法。现在的问题是把msvcr90.dll放在哪里以便Apache可以使用它?我注意到 Apache 的 bin 文件夹包含 msvcr70.dll 和 msvcr80.dll,但在其中放入 90 不起作用。