简体   繁体   中英

function error for python-rsa module

i installed Python-RSA for using rsa algorithm in my python project , but when i want to use it i have this error :

运行错误

import rsa
(public_key, private_key) = rsa.newkeys(1024, accurate=True, poolsize=8)

Above code is from official Doc

i check module files for newkeys() function and this function exists

模块的文件

def newkeys(nbits, accurate=True, poolsize=1):
    if nbits < 16:
        raise ValueError('Key too small')
    if poolsize < 1:
        raise ValueError('Pool size (%i) should be >= 1' % poolsize)
    if poolsize > 1:
        from rsa import parallel
        import functools
        getprime_func = functools.partial(parallel.getprime, poolsize=poolsize)
    else: getprime_func = rsa.prime.getprime
    (p, q, e, d) = gen_keys(nbits, getprime_func)
    n = p * q
    return (PublicKey(n, e), PrivateKey(n, e, d, p, q))
__all__ = ['PublicKey', 'PrivateKey', 'newkeys']
if __name__ == '__main__':
    import doctest
    try:
        for count in range(100):
            (failures, tests) = doctest.testmod()
            if failures:
                break
            if (count and count % 10 == 0) or count == 1:
                print('%i times' % count)
    except KeyboardInterrupt:
        print('Aborted')
    else:
        print('Doctests done')

what's wrong here ?

So, as it turns out, you ran into a rather strange property of Python, namely that you can import the module of the current file. Your rsa.py file was shadowing the rsa directory (which contains an __init__.py file so it can be loaded as a module.)

Try making a file foo.py with the following contents

import foo

def bar():
    return 5

print dir(foo)

This will print something like

['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'bar', 'foo']
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'bar', 'foo']

when you run python foo.py

It prints the directory structure twice (once when the module is loaded) and once when the file is evaluated.

This does not result in an infinite loop because Python caches modules that it has already loaded.

thanks to Gregory :

1- first thing , the *.py file name and imported module name can not be the same .... if we use the same name for them problem like mine will come. IDLE can not recognize the write way to run the file

2- there is a seriously problem in this module. when i want use poolsize=x parameter of newkeys() function i have many error and a big loop in my program. it's because multiprocessing of this module that not work in Windows 10. This problem can be caused by Windows or module .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM