如何在pyinstaller中只包含需要的模块? [英] How to include only needed modules in pyinstaller?

查看:52
本文介绍了如何在pyinstaller中只包含需要的模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用pyinstaller为我的单个python文件生成.exe文件,但是文件大小超过30MB,启动非常慢。从我收集到的信息来看,pyinstaller默认情况下捆绑了很多不需要的东西。有没有一种方法可以确保pyinstaller找出只需要什么并只捆绑它们?我的脚本上的导入部分如下所示:

import datetime
import os
import numpy as np
import pandas as pd 
import xlsxwriter
from tkinter import *

编辑:

或者有没有办法查看它捆绑的所有模块的列表?这样我就可以检查它们并排除我不需要的那些。

推荐答案

我最终使用了cx_Freeze。它似乎比py2exepyinstaller工作得好得多。我编写的setup.py文件如下所示:

import os
import shutil
import sys
from cx_Freeze import setup, Executable

os.environ['TCL_LIBRARY'] = r'C:inPython37-32	cl	cl8.6'
os.environ['TK_LIBRARY'] = r'C:inPython37-32	cl	k8.6'

__version__ = '1.0.0'
base = None
if sys.platform == 'win32':
    base = 'Win32GUI'

include_files = ['am.png']
includes = ['tkinter']
excludes = ['matplotlib', 'sqlite3']
packages = ['numpy', 'pandas', 'xlsxwriter']

setup(
    name='TestApp',
    description='Test App',
    version=__version__,
    executables=[Executable('test.py', base=base)],
    options = {'build_exe': {
        'packages': packages,
        'includes': includes,
        'include_files': include_files,
        'include_msvcr': True,
        'excludes': excludes,
    }},
)

path = os.path.abspath(os.path.join(os.path.realpath(__file__), os.pardir))
build_path = os.path.join(path, 'build', 'exe.win32-3.7')
shutil.copy(r'C:inPython37-32DLLs	cl86t.dll', build_path)
shutil.copy(r'C:inPython37-32DLLs	k86t.dll', build_path)

任何人都可以运行python setup.py build_exe生成可执行文件,或运行python setup.py bdist_msi生成安装程序。

这篇关于如何在pyinstaller中只包含需要的模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆