问题描述
如何解决
from passlib.hash import sha256_crypt ImportError: No module named'通行证'
我已经安装在使用 pip install passlib它说
I have already installed in using pip install passliband it says
要求已经满足(使用--upgrade升级):passlib inc:\python34\lib\site-packages 清理中...
你如何解决这个问题
谢谢
推荐答案
passlib 存在导入解析问题",但我预计它不会找到 sha256_crypt 而不是找不到 passlib.首先,我会确保您的机器上正确安装了 passlib 模块.其次,我会尝试运行出现错误的程序,看看您是否可以运行以下内容:
There is an import resolution "issue" with passlib, but I expected that it would not find sha256_crypt instead of not finding passlib.Firstly, I would ensure that you have the passlib module properly installed on your machine. Secondly, I would try to run the program with the error and see if you can run something like:
sha256_crypt.encrypt("someString")
如果运行,那么唯一的问题"是导入解析是静态的,它无法解析在运行时未定义的函数.如果您查看 passlib 中的 hash.py,这将是有意义的.
If that runs, then the only "problem" is that the import resolution is static and it cannot resolve functions which are not defined at run time. This will make sense if you take a look at hash.py from passlib.
# NOTE: could support 'non-lazy' version which just imports
# all schemes known to list_crypt_handlers()
#=============================================================================
# import proxy object and replace this module
#=============================================================================
from passlib.registry import _proxy
import sys
sys.modules[__name__] = _proxy
#=============================================================================
# eoc
#=============================================================================
如您所见,sha256_crypt 未在此处定义,因此导入返回错误,即使模块在运行时正确加载!
As you can see, sha256_crypt is not defined here, so the import comes back as being wrong, even though the module will load correctly at run time!
此时您有两个选择.如果你像我一样使用 PyDev,你可以在导入旁边添加一个忽略标志:
You have two options at this point. If you are using PyDev like I am, you can add an ignore flag next to the import:
from passlib.hash import sha256_crypt #@UnresolvedImport
您还可以修改 hash.py 以便定义占位符 sha256_crypt 函数以确保导入返回有效,但这确实不是最佳方法,但确实有效:
You can also modify hash.py such that you define a placeholder sha256_crypt function to ensure that the import comes back as valid, but really this is not the best approach, but it does work:
# NOTE: could support 'non-lazy' version which just imports
# all schemes known to list_crypt_handlers()
#=============================================================================
# import proxy object and replace this module
#=============================================================================
def sha256_crypt():
pass
from passlib.registry import _proxy
import sys
sys.modules[__name__] = _proxy
#=============================================================================
# eoc
#=============================================================================
这将确保导入解析过程会看到该函数存在并且不会抱怨.
That will ensure that the import resolution process will see that the function exists and it will not complain.
这篇关于没有名为“passlib"的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!