You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows Server2022部署DRF时cryptography _rust导入DLL失败求助

解决Windows Server 2022+Apache2环境下cryptography的DLL加载失败问题

问题描述

本地Windows10+Python3.10.11虚拟环境代码运行正常,但部署到Windows Server2022+Apache2+Python3.11.3虚拟环境时出现以下错误:

from cryptography.hazmat.primitives.asymmetric import padding
  File "C:\cbsesb\cbsenv\Lib\site-packages\cryptography\hazmat\primitives\asymmetric\padding.py", line 9, in <module>
    from cryptography.hazmat.primitives import hashes
  File "C:\cbsesb\cbsenv\Lib\site-packages\cryptography\hazmat\primitives\hashes.py", line 9, in <module>
    from cryptography.hazmat.bindings._rust import openssl as rust_openssl
ImportError: DLL load failed while importing _rust: The specified module could not be found.

当前环境关键依赖:cffi-1.17.1、cryptography-43.0.1、pycparser-2.22,完整依赖列表:

Package                 Version
----------------------- ---------
asgiref                 3.7.1
certifi                 2023.7.22
cffi                    1.17.1
charset-normalizer      3.3.2
cryptography            43.0.1
cx-Oracle               8.3.0
defusedxml              0.7.1
dicttoxml               1.7.16
Django                  4.2.1
djangorestframework     3.14.0
djangorestframework-xml 2.0.0
idna                    3.6
ldap3                   2.9.1
lxml                    4.9.3
mod-wsgi                4.9.4
pip                     24.2
psycopg2                2.9.6
pyasn1                  0.6.0
pycparser               2.22
pycryptodome            3.18.0
pyOpenSSL               24.2.1
pytz                    2023.3
requests                2.31.0
setuptools              74.1.2
signxml                 3.2.1
soupsieve               2.5
sqlparse                0.4.4
typing_extensions       4.6.1
tzdata                  2023.3
urllib3                 2.2.0
xmltodict               0.12.0

已尝试但未解决的操作:

  • 升级pip和setuptools:python -m pip install --upgrade pip setuptools
  • 将Python降级至3.10
  • 安装Rust
  • 安装cryptography二进制包:python -m pip install cryptography --only-binary cryptography

解决步骤

1. 安装Visual C++ Redistributable运行时库

cryptography的Rust绑定依赖Visual C运行时组件,Windows Server 2022默认可能缺失对应版本。下载并安装**Microsoft Visual C Redistributable for Visual Studio 2022(x64版本,需与Python位数一致)**,安装完成后重启服务器再测试。

2. 定位并补充缺失的DLL文件

用Dependency Walker工具检查_rust.pyd依赖的系统DLL:

  • 找到虚拟环境下的C:\cbsesb\cbsenv\Lib\site-packages\cryptography\hazmat\bindings\_rust.pyd文件
  • 用Dependency Walker打开该文件,查看标注为缺失的DLL
  • 从本地正常运行的Windows10环境中复制对应DLL到Windows Server2022的C:\Windows\System32目录,或虚拟环境的Scripts目录

3. 降级cryptography版本

新版本cryptography对Rust绑定的兼容性要求更高,尝试降级到稳定兼容版本:

pip uninstall cryptography
pip install cryptography==41.0.7

4. 配置Apache的环境变量路径

Apache进程可能未加载虚拟环境的DLL路径,在Apache配置文件中添加:

SetEnv PATH "C:\cbsesb\cbsenv\Scripts;%PATH%"

保存配置后重启Apache服务。

5. 本地编译cryptography包

若上述方法无效,尝试卸载现有包后从源码编译(需提前安装Visual Studio Build Tools和Rust):

pip uninstall cryptography
pip install cryptography --no-binary :all:

编译过程会自动适配当前Windows Server环境生成匹配的绑定文件。


内容的提问来源于stack exchange,提问作者Alingan Patra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 11:16:14