以下环境为python3.6.5, 在virtualenv下实现
-
pyinstaller采用3.3.1版本
pip install pyinstaller==3.3.1
-
setuptools采用44.0.0版本
pip install setuptools==44.0.0
-
由于依赖了crypto库用于加密,但是pyinstaller库延续开发的人漏了hook,需要手动加一个hook文件到C:\python36\Lib\site-packages\PyInstaller\hooks,文件名是hook-Crypto.py,内容如下:
#----------------------------------------------------------------------------- # 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
-
执行打包命令
pyinstaller main.py -F -p C:\..\venv\Lib\site-packages
-
附上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