Python导入cryptography.fernet模块失败问题求助
解决cryptography模块导入失败问题(Miniconda环境)
先确认你用的Python解释器属于Miniconda环境
打开终端/命令提示符,执行python --version,查看输出的路径是否指向Miniconda安装目录(比如~/miniconda3/bin/python或C:\xx\miniconda3\python.exe)。如果不是,需先激活Miniconda环境:- Windows:打开Miniconda Prompt(默认激活base环境),或在普通cmd中输入
conda activate base - Linux/macOS:终端输入
source ~/miniconda3/bin/activate base(路径按实际安装位置调整)
- Windows:打开Miniconda Prompt(默认激活base环境),或在普通cmd中输入
检查当前环境是否已安装cryptography
激活环境后,执行conda list或pip list,查找cryptography条目。如果未找到,在激活的环境中执行conda install cryptography(更适配Miniconda环境)或pip install cryptography重新安装。验证导入是否正常
激活环境后,输入python进入交互模式,执行from cryptography.fernet import Fernet。如果无报错,说明环境配置正常,再运行你的代码:from cryptography.fernet import Fernet key = Fernet.generate_key() cipher_suite = Fernet(key) message = b"Secret message" encrypted_message = cipher_suite.encrypt(message) print("Key:", key) print("Encrypted Message:", encrypted_message) cipher_suite = Fernet(key) decrypted_message = cipher_suite.decrypt(encrypted_message) print("Decrypted Message:", decrypted_message.decode())适配IDE的解释器设置
如果用IDE运行代码,需确保IDE选择的Python解释器是Miniconda环境的:- VSCode:点击左下角状态栏的Python版本,选择Miniconda的base环境
- PyCharm:进入
File -> Settings -> Project: 你的项目名 -> Python Interpreter,添加Miniconda目录下的Python路径
内容的提问来源于stack exchange,提问作者Kevin Tony
相关产品推荐
相关产品推荐

