如何解决Python中‘No module named cryptography’报错问题
解决Python中cryptography模块导入失败问题
问题场景
运行Python连接MySQL的程序时,系统提示需要cryptography包用于SHA256密码处理,但添加import cryptography语句后,出现no module named cryptography错误,且已执行过pip install cryptography安装命令。
解决方案
检查Python环境与pip的对应关系
系统中存在多个Python版本时,pip安装的包可能不在当前运行的Python环境中。- 执行
which python(Linux/macOS)或where python(Windows),查看当前运行的Python路径; - 执行
which pip(Linux/macOS)或where pip(Windows),查看pip对应的路径,确保两者一致; - 若不一致,使用对应版本的pip重新安装:
python3 -m pip install cryptography(Linux/macOS)或py -3 -m pip install cryptography(Windows)。
- 执行
强制重新安装cryptography
若安装过程中文件损坏,执行pip install --force-reinstall cryptography重新安装包。检查虚拟环境(若使用)
若代码在虚拟环境中运行,需激活虚拟环境后再安装包,或直接使用虚拟环境内的pip:- Linux/macOS:
venv/bin/pip install cryptography - Windows:
venv\Scripts\pip install cryptography
- Linux/macOS:
验证安装结果
打开Python交互环境,输入import cryptography,无报错则说明安装成功;也可执行pip list查看已安装包列表,确认cryptography存在。
额外代码问题提示
你的代码中insert语句存在参数不匹配问题:
cur.execute("insert into student_register (f_name,l_name,email,question,answer,password) values(%s,%s,%s,%s,%s,%s)", ( self.username_txt.get(), self.password_txt.get() ))
SQL语句需要6个参数,但仅传入2个,运行时会触发错误,需补充对应字段的参数。
用户原始代码
from tkinter import * from tkinter import ttk, messagebox import pymysql, os import credentials as cr import hashlib import cryptography class SignUp: def __init__(self, root): self.window = root self.window.title("Sign Up") self.window.geometry("1280x800+0+0") self.window.config(bg = "white") frame = Frame(self.window, bg="white") frame.place(x=350,y=100,width=500,height=550) title1 = Label(frame, text="Sign Up", font=("times new roman",25,"bold"),bg="white").place(x=20, y=10) title2 = Label(frame, text="play with bangtan", font=("times new roman",13),bg="white", fg="gray").place(x=20, y=50) self.username_txt = Entry(frame,font=("arial")) self.username_txt.place(x=20, y=130, width=200) password = Label(frame, text="New password", font=("helvetica", 15 ,"bold") ,bg="white").place(x=20, y=340) self.password_txt = Entry(frame,font=("arial")) self.password_txt.place(x=20, y=370, width=420) self.signup = Button(frame, text="Sign Up", command=self.signup_func, font=("times new roman", 18, "bold"), bd=0, cursor="hand2", bg="green2", fg="white").place(x=120, y=470, width=250) def signup_func(self): if self.username_txt.get()=="" or self.password_txt.get() == "": messagebox.showerror("Error!","Sorry!, All fields are required",parent=self.window) else: try: connection = pymysql.connect(host="localhost", user='user1', password='root1@11', database="logindata") cur = connection.cursor() cur.execute("select * from userdata where username_text=%s",self.username_txt.get()) row=cur.fetchone() # Check if th entered email id is already exists or not. if row!=None: messagebox.showerror("Error!","The email id is already exists, please try again with another email id",parent=self.window) else: cur.execute("insert into student_register (f_name,l_name,email,question,answer,password) values(%s,%s,%s,%s,%s,%s)", ( self.username_txt.get(), self.password_txt.get() )) connection.commit() connection.close() messagebox.showinfo("Congratulations!","Register Successful",parent=self.window) self.reset_fields() except Exception as es: messagebox.showerror("Error!",f"Error due to {es}",parent=self.window) def reset_fields(self): self.username_txt.delete(0, END) self.password_txt.delete(0, END) if __name__ == "__main__": root = Tk() obj = SignUp(root) root.mainloop()
内容的提问来源于stack exchange,提问作者chetana
相关产品推荐
相关产品推荐

