用pyinstaller打包exe注意事項(xiàng)

以下環(huán)境為python3.6.5, 在virtualenv下實(shí)現(xiàn)

  1. pyinstaller采用3.3.1版本

    pip install pyinstaller==3.3.1

  2. setuptools采用44.0.0版本

    pip install setuptools==44.0.0

  3. 由于依賴了crypto庫(kù)用于加密,但是pyinstaller庫(kù)延續(xù)開(kāi)發(fā)的人漏了hook,需要手動(dòng)加一個(gè)hook文件到C:\python36\Lib\site-packages\PyInstaller\hooks,文件名是hook-Crypto.py,內(nèi)容如下:

    
    #-----------------------------------------------------------------------------
    # Copyright (c) 2005-2018, PyInstaller Development Team.
    #
    # Distributed under the terms of the GNU General Public License with exception
    # for distributing bootloader.
    #
    # The full license is in the file COPYING.txt, distributed with this software.
    #-----------------------------------------------------------------------------
     
    """
    Hook for PyCryptodome library: https://pypi.python.org/pypi/pycryptodome
    PyCryptodome is an almost drop-in replacement for the now unmaintained
    PyCrypto library. The two are mutually exclusive as they live under
    the same package ("Crypto").
    PyCryptodome distributes dynamic libraries and builds them as if they were
    Python C extensions (even though they are not extensions - as they can't be
    imported by Python). It might sound a bit weird, but this decision is rooted
    in PyPy and its partial and slow support for C extensions. However, this also
    invalidates several of the existing methods used by PyInstaller to decide the
    right files to pull in.
    Even though this hook is meant to help with PyCryptodome only, it will be
    triggered also when PyCrypto is installed, so it must be tested with both.
    Tested with PyCryptodome 3.5.1, PyCrypto 2.6.1, Python 2.7 & 3.6, Fedora & Windows
    """
     
    import os
    import glob
     
    from PyInstaller.compat import EXTENSION_SUFFIXES
    from PyInstaller.utils.hooks import get_module_file_attribute
     
    # Include the modules as binaries in a subfolder named like the package.
    # Cryptodome's loader expects to find them inside the package directory for
    # the main module. We cannot use hiddenimports because that would add the
    # modules outside the package.
     
    binaries = []
    binary_module_names = [
        'Crypto.Math',      # First in the list
        'Crypto.Cipher',
        'Crypto.Util',
        'Crypto.Hash',
        'Crypto.Protocol',
    ]
     
    try:
        for module_name in binary_module_names:
            m_dir = os.path.dirname(get_module_file_attribute(module_name))
            for ext in EXTENSION_SUFFIXES:
                module_bin = glob.glob(os.path.join(m_dir, '_*%s' % ext))
                for f in module_bin:
                    binaries.append((f, module_name.replace('.', os.sep)))
    except ImportError:
        # Do nothing for PyCrypto (Crypto.Math does not exist there)
        pass
    
  4. 執(zhí)行打包命令

    pyinstaller main.py -F -p C:\..\venv\Lib\site-packages
    
  5. 附上requirement.txt

    altgraph==0.17
    certifi==2019.11.28
    cffi==1.14.0
    chardet==3.0.4
    cryptography==2.8
    future==0.18.2
    idna==2.8
    macholib==1.14
    Naked==0.1.31
    pefile==2019.4.18
    pycparser==2.19
    pycryptodome==3.9.6
    PyInstaller==3.3.1
    pywin32-ctypes==0.2.0
    PyYAML==5.3
    requests==2.22.0
    shellescape==3.8.1
    six==1.14.0
    urllib3==1.25.8
    
    

    ?

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容