paarth batra
paarth batra

Reputation: 1412

passlib.hash import sha256_crypt gives error when compiled with py2exe

I have a code written in Python where I am hashing the password using passlib.hash, sha256_crypt and then later verifying the password using the same library .

I am able to run the code using the console i.e. using .py.

My problem occurs when I compile this program using py2exe:

ImportError: No module named passlib.hash

I am importing the module using following command :

from passlib.hash import sha256_crypt

and sometimes I see a warning saying no module namedsha256_crypt in the GUI but the program still runs correctly.

I am using Windows 7 and could not find any solution to this problem. I have checked my python home directory it seems on installing passlib-1.6.1 , passlib-1.6.1-py2.7.egg file is created under C:\Python2.7.5\Lib\site-packages, however there are no files named passlib/hash or sha_256.

Upvotes: 2

Views: 3481

Answers (2)

jmodrak
jmodrak

Reputation: 229

Eli Collins is actually right and pointed me in the right direction.

You can solve your problem by typing the import more specifically. I solved my sha512 problem by changing the import to be looking like this

from passlib.handlers.sha2_crypt import sha512_crypt

My py2exe distribution now works very well.

I know it's a little late for you, but it still might help someone, like it helped me. Kudos for having filed the question and also kudos to Eli!

Upvotes: 3

Eli Collins
Eli Collins

Reputation: 8533

In order to load only the hashers which have been explicitly requested, Passlib plays a dynamic import trick: passlib.hash is actually a special object which only imports each hasher class when requested ... the real hasher classes are actually stored in modules over in passlib's internal package passlib.handlers (in particular, passlib.hash:sha256_crypt is actually stored under passlib.handlers.sha2_crypt:sha256_crypt).

Py2exe on the other hand try to be smart, and tries to walk your application's import tree, and only bundle the modules which are actually loaded. I find it frequently (and understandably) fouls up whenever it runs into a python package pulling dynamic import tricks (like the above). This ends up with the py2exe-packaged apps giving strange and hard to track down import errors.

I'm not positive this will fix your problem, but the easiest solution I know of is to tell py2exe to include the entire passlib package, and not bother trying to "guess" which parts should be included. This can be done by adding "passlib" to py2exe's packages option...

setup(
    # ... other stuff ...
    options={
            "py2exe":{
                    # ... other stuff ...
                    "packages": ["passlib"],
            }
    }
)

Upvotes: 2

Related Questions