Skip to content Skip to sidebar Skip to footer

No Module Named 'passlib'

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

Solution 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.hashimport 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#=============================================================================defsha256_crypt():
        passfrom 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.

Post a Comment for "No Module Named 'passlib'"