Pyinstaller 打包.py生成.exe的方法和报错总结

Pyinstaller 打包.py生成.exe的方法和报错总结


简介

有时候自己写了个python脚本觉得挺好用想要分享给小伙伴,但是每次都要帮他们的电脑装个python环境。虽然说装一下也快,但是相对来说效率还是不高,要是能将python的**.py文件转化为.exe**,那么世界将变得更美好。这篇文章我将简单的介绍如何使用Pyinstaller来打包我们的python脚本。

安装 Pyinstaller

pyinstaller的官网为:http://www.pyinstaller.org/

如果有什么问题一般都能够在这里找到解答。

安装 pyinstaller还是非常的方便的,只需要一句话:

pip install pyinstaller

就OK了

打包 Python脚本

关于打包其实也很简单,也只需要一句话:

pyinstaller yourprogram.py

当然pyinstaller命令的语法是:

pyinstaller [options] script [script ...] | spec文件

在运行 pyinstaller命令后在yourprogram.py同一个目录下会产生一个文件两个文件夹:

  1. yourprogram.spec文件,该文件中写了一些配置,可以打开进行修改,下一次可以直接打包该文件,不用打包yourprogram.py了。
  2. build文件夹,包含一些日志文件和工作文件
  3. dist文件夹,包含可执行文件

1. 控制台窗口选项

-c, --console, --nowindowed 打包程序运行时,打开标准i / o控制台窗口(默认) 
-w, --windowed, --noconsole 打包程序运行时,不打开标准i / o控制台窗口(默认) 

2. 软件图标选项

-i FILE.ico , --icon FILE.ico 为你的程序添加一个图标

3. 版本信息选项

--version-file FILE.txt 可以将您的版本信息添加到你的软件当中

这个FILE.txt的文件内容为:

# UTF-8
#
VSVersionInfo(
  ffi=FixedFileInfo(
    # filevers and prodvers should be always a tuple with four items: (1, 2, 3, 4)
    # Set not needed items to zero 0.
    filevers=(10, 0, 17134, 1),
    prodvers=(10, 0, 17134, 1),
    # Contains a bitmask that specifies the valid bits 'flags'r
    mask=0x3f,
    # Contains a bitmask that specifies the Boolean attributes of the file.
    flags=0x0,
    # The operating system for which this file was designed.
    # 0x4 - NT and there is no need to change it.
    OS=0x40004,
    # The general type of file.
    # 0x1 - the file is an application.
    fileType=0x1,
    # The function of the file.
    # 0x0 - the function is not defined for this fileType
    subtype=0x0,
    # Creation date and time stamp.
    date=(0, 0)
    ),
  kids=[
    StringFileInfo(
      [
      StringTable(
        u'040904B0',
        [StringStruct(u'CompanyName', u'Microsoft Corporation'),
        StringStruct(u'FileDescription', u'Windows Command Processor'),
        StringStruct(u'FileVersion', u'10.0.17134.1 (WinBuild.160101.0800)'),
        StringStruct(u'InternalName', u'cmd'),
        StringStruct(u'LegalCopyright', u'© Microsoft Corporation. All rights reserved.'),
        StringStruct(u'OriginalFilename', u'Cmd.Exe'),
        StringStruct(u'ProductName', u'Microsoft® Windows® Operating System'),
        StringStruct(u'ProductVersion', u'10.0.17134.1')])
      ]),
    VarFileInfo([VarStruct(u'Translation', [1033, 1200])])
  ]
)

4. 生成结果选项

-D,--onedir 这会创建一个包含可执行文件的单文件夹包(默认)
-F,--onefile 这个只会创建一个可执行文件
--specpath DIR 用于存储生成的spec文件的文件夹(默认值:当前目录)
-n NAME, --name NAME 应用程序的名称(默认值:第一个脚本的名称)

以上四个就是常用的参数,可以根据自己的需求进行设置。

例子

下面这个是我打包一个“自动更新桌面的脚本”的命令:

G:\PyinstallerTest\Auto_Wallpaper>pyinstaller -F -i G:\PyinstallerTest\Auto_Wallpaper\icon\moon.ico --version-file=version.txt Auto_Wallpaper_of_Moon.py

执行后就能够成功的将**.py脚本转化为.exe**可执行文件。

截图如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pVD3tlXE-1604558877567)(https://i.imgur.com/lsD4QJg.jpg)]

错误总结

在这里我会总结我在使用pyinstaller过程中出现的错误和解决方法,我会在这里不断的更新我的内容,如果有需要可以关注一下哦。

1. AttributeError: ‘str’ object has no attribute ‘items’

错误内容截取:

for real_module_name, six_module_name in real_to_six_module_name.items():
AttributeError: 'str' object has no attribute 'items'

解决方法:

你需要更新一下你的setuptools

pip install --upgrade setuptools

2. 打包后运行.exe程序调试闪退

有时候将.py文件打包成.exe文件后,不一定能够正常运行。但是点.exe后程序一闪而退,就不知道问题出在哪里。

解决方法:

在cmd行里面运行你的.exe文件

3. 重复打包的时候显示No module named “XXX”

当我们第一次打包一个程序的时候,有可能会提示:No module named “XXX” 。然后我们会去安装相应缺少的包,装好后第二次准备打包,发现还是提示:No module named “XXX”。

解决方法:

删除你第一次打包残留的文件,特别是./build 目录下的所有文件。

4. 打包后发现文件过大

一个就几十行的程序,打包后可能会发现有一两百个MB。

解决方法:

如果你使用Anaconda中的某个已经装了很多包的环境,就很可能出现这个问题。这个时候只要再创建一个新的环境,只安装所依赖的包即可。

最后

希望对大家有所帮助!:-)

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值