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

虚拟环境中Python脚本函数调用异常:cryptography模块找不到

虚拟环境中调用cryptkey函数触发cryptography模块找不到的错误

在虚拟环境中,调用下述Python脚本的generateKey函数可以正常生成密钥文件,但调用cryptkey函数时却抛出No module named 'cryptography'错误。

脚本代码:

import cryptography.fernet

def generateKey(keyfile : str):
    key = cryptography.fernet.Fernet.generate_key()

    with open (keyfile, "bw") as fp:
        fp.write (key)

def cryptkey(stringData : str, keyfile : str) -> str:
    symkey = None

    with open (keyfile,"r") as fp:
        symkey = fp.read()

    crypter      = cryptography.fernet.Fernet(symkey)
    crypted_text = crypter.encrypt(stringData.encode())

    return(crypted_text.decode())

已确认虚拟环境中安装了cryptography 41.0.3版本,Python版本为3.10.11。尝试过以下操作仍无法解决问题:

  • 重装cryptography模块
  • 删除所有__pycache__文件夹
  • 重启电脑
  • 删除并重建虚拟环境

调用命令及输出:
执行以下命令时一切正常:

(venv) E:\Projekte...>python -c "import keyFunctions.keyFunctions as kf; kf.generateKey('keyfile.txt')"

但执行这条命令时抛出错误:

(venv) E:\Projekte...>python3 -c "import keyFunctions.keyFunctions as kf; print(kf.cryptkey('blubb', 'keyfile.txt'));"

错误信息如下:

Traceback (most recent call last):
File "", line 1, in 
File "E:\Projekte...\keyFunctions\keyFunctions.py", line 1, in 
from cryptography import fernet
ModuleNotFoundError: No module named 'cryptography'

内容的提问来源于stack exchange,提问作者Wolfgang Ebert

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 23:25:08