【问题标题】:PyInstaller not correctly importing pycrypto... sometimesPyInstaller 无法正确导入 pycrypto... 有时
【发布时间】:2014-04-25 09:20:45
【问题描述】:

我在不同的 ubuntu 机器上使用 PyInstaller 打包一个项目。 其中一些,在执行生成的项目时,会抛出这个错误:

文件 "~/PyInstaller-2.1/proj/build/proj/out00-PYZ.pyz/Crypto.Random", 第 28 行,在 ImportError: cannot import name OSRNG

但是,导入在 python 控制台中完全正常,我可以在不打包的情况下执行项目。

我尝试卸载并重新安装 pycrypto 没有成功,我也尝试添加特定的

从 Crypto.Random 导入 OSRNG

到主文件,以便 PyInstaller 将其拾取。

【问题讨论】:

    标签: python python-2.7 pyinstaller pycrypto


    【解决方案1】:

    我能够用 hithwen 的食谱解决这个问题,但使用的 .spec 文件略有不同。留在这里供大家参考。

    # -*- mode: python -*-
    
    #Tweaks to properly import pyCrypto
    
    #Get the path
    def get_crypto_path():
        '''Auto import sometimes fails on linux'''
        import Crypto
        crypto_path = Crypto.__path__[0]
        return crypto_path
    
    #Analysis remains untouched
    a = Analysis(['myapp.py'],
                 pathex=[],
                 hiddenimports=[],
                 hookspath=None,
                 runtime_hooks=None)
    #Add to the tree the pyCrypto folder
    dict_tree = Tree(get_crypto_path(), prefix='Crypto', excludes=["*.pyc"])
    a.datas += dict_tree
    #As we have the so/pyd in the pyCrypto folder, we don't need them anymore, so we take them out from the executable path
    a.binaries = filter(lambda x: 'Crypto' not in x[0], a.binaries)
    #PYZ remains untouched
    pyz = PYZ(a.pure)
    #EXE remains untouched
    exe = EXE(pyz,
              a.scripts,
              exclude_binaries=True,
              name='myapp',
              debug=False,
              strip=None,
              upx=True,
              console=True )
    #COLLECT remains untouched
    coll = COLLECT(exe,
                   a.binaries,
                   a.zipfiles,
                   a.datas,
                   strip=None,
                   upx=True,
                   name='myapp')
    

    【讨论】:

      【解决方案2】:

      我已经通过将加密目录树添加到规范文件来解决它

      我得到了这个函数的路径:

      def get_crypto_path():
          '''Auto import sometimes fails on linux'''
          import Crypto
          crypto_path = Crypto.__path__[0]
          return crypto_path
      

      然后在spec文件中替换:

      dict_tree = Tree('CRYPTO_PATH', prefix='Crypto', excludes=["*.pyc"])
      a.datas += dict_tree
      

      【讨论】:

        【解决方案3】:

        我通过将 pycrypto / pycryptodome 替换为 pycryptodomex 来实现它。分享已发布答案的链接:https://stackoverflow.com/a/50009769/4355695

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-05
          相关资源
          最近更新 更多