Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ImportError:没有名为cryptography.fernet的模块_Python_Cryptography_Python Import_Importerror_Traceback - Fatal编程技术网

Python ImportError:没有名为cryptography.fernet的模块

Python ImportError:没有名为cryptography.fernet的模块,python,cryptography,python-import,importerror,traceback,Python,Cryptography,Python Import,Importerror,Traceback,我在Python 2中有以下脚本,crypto.py: import hashlib from cryptography.fernet import Fernet def make_hash(password): return hashlib.sha224(password).hexdigest() def check_hash(password, hash): """Check a password against an existing hash.""" retu

我在Python 2中有以下脚本,
crypto.py

import hashlib
from cryptography.fernet import Fernet

def make_hash(password):
    return hashlib.sha224(password).hexdigest()

def check_hash(password, hash):
    """Check a password against an existing hash."""
    return hashlib.sha224(password).hexdigest() == hash

def start():
    key = Fernet.generate_key()
    print key
    cipher_suite = Fernet(key)
    print cipher_suite
    return cipher_suite

def crypt(message):
    global cipher_suite
    cipher_suite=start()
    cipher_text = cipher_suite.encrypt(message)
    return cipher_text
print "123"

def decrypt(cipher_text):
    plain_text = cipher_suite.decrypt(cipher_text)
    return plain_text
print "456"

message = "Hello!"
print crypt(message)
print decrypt(crypt(message))
运行此脚本时,我得到以下输出:

123
456
Hgir1BHvlLLMUH-Xi-aDrtNFcT3XU86XQsWtrvn6S2s=
<cryptography.fernet.Fernet object at 0x01661A10>
gAAAAABYRAxpvX9ksY5HVNiVa__S9zfBtV0XvVjUS9RpOOJhLp0fVZPbnk1hNMk9xB9x_s88WDRNF14GhY7DJG7B7g0ngIrENA==
cUKBKF-dsP-sQ5_BN1H6yuq_t1h-kBbBgf6N-LCrynM=
<cryptography.fernet.Fernet object at 0x01BDB5D0>
Hello!
当我运行
client.py
脚本时,我得到以下消息:

Traceback (most recent call last):
  File "K:/A/Project/client.py", line 6, in <module>
    import threading, socket, crypto
  File "K:\A\Project\crypto.py", line 2, in <module>
    from cryptography.fernet import Fernet
ImportError: No module named cryptography.fernet
回溯(最近一次呼叫最后一次):
文件“K:/A/Project/client.py”,第6行,在
导入线程、套接字、加密
文件“K:\A\Project\crypto.py”,第2行,在
从cryptography.fernet导入fernet
ImportError:没有名为cryptography.fernet的模块

2.5版是一个解决方案:

pip install cryptography==2.5
(见附件)

然后,为了避免“ImportError:No module named enum”错误,请使用python3

您可能需要安装enum34,但我不需要安装。
(参见)

插入
导入系统;打印(sys.path)
就在
之前,从cryptography.fernet导入fernet
crypto.py
。然后重新运行
crypto.py
client.py
。在
系统路径中查找差异。确保安装
加密
包的目录列在
sys.path
@unutbu I get
len(sys.path)
=8 from
client.py
和from
crypto.py
I get 9中。但是,第一项在第二个
sys.path
中重复。i、 e.没有区别。@unutbu打印时,我在两个脚本中得到相同的目录:
import os
dir\u path=os.path.dirname(os.path.realpath(\uu file\uuuuu))
print dir\u path
如果输入
密码,会发生什么;在
crypto.py
中打印(加密文件)
,然后重新运行
crypto.py
client.py
?根据
sys.path
中目录的顺序,Python可能找到了名为
cryptography
的错误包或模块。打印
cryptography.\u_文件\u_
将帮助您查看Python正在查找的模块或包(如果有的话)。
pip install cryptography==2.5