No module named 'passlib'

左心房为你撑大大i 提交于 2019-12-11 11:46:22

问题


How to fix

from passlib.hash import sha256_crypt ImportError: No module named 'passlib'

I have already installed in using pip install passlib and it says

Requirement already satisfied (use --upgrade to upgrade): passlib in c:\python34\lib\site-packages Cleaning up...

How do you fix this

thanks


回答1:


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")

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
#=============================================================================

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!

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

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.



来源:https://stackoverflow.com/questions/33915448/no-module-named-passlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!