pyinstaller 打包.py文件为.exe

 

1.首先分别pip install pyinstaller 和 pip install pywin32[对于不能pip install pywin32,可以去官网下载对应版本]

pyinstaller官方文档:https://pyinstaller.readthedocs.io/en/stable/

django打包的官方文档:https://github.com/pyinstaller/pyinstaller/wiki/Recipe-Executable-From-Django

-F     指定打包后只生成一个exe格式的文件
-D     –onedir 创建一个目录,包含exe文件,但会依赖很多文件(默认选项)
-c     –console, –nowindowed 使用控制台,无界面(默认)
-w     –windowed, –noconsole 使用窗口,无控制台
-p     添加搜索路径,让其找到对应的库。
-i     改变生成程序的icon图标

2.对于直接在命令行执行pyinstaller -F 'path'\**.py会在相应scripts下生成三个文件build,dist,**.spec,这样可能会导致.exe程序不能执行,所以最好是将编译生成的文件放在.py的同一项目目录下,否则可能会报错:failed to script**;VCRUNTIME140.dll等等(当然不排除其他原因导致)

这里参考其他博客https://blog.csdn.net/pipisorry/article/details/50620122关于pyinstaller的更详细解释也可参考此博客,感觉直接写一个run.py文件的方式是最简单也是最直观的

#coding:utf-8
from PyInstaller.__main__ import run

if __name__ == '__main__':
    opts1 = ['test1.py', '-F']#其中test1.py为自己想要打包的项目
    # opts2 = ['test1.py', '-F', '-w']
    # opts3 = ['test1.py', '-F','--upx-dir','upx-3.95-win64']
    run(opts)

#这里‘upx-3.95-win64’是将各种依赖包等进行压缩的工具的文件名,可通过http://upx.sourceforge.net/下载,下载后最好将其解压至同一项目目录下,

注意:有时候选择上述代码中opts3时可能会报错xxx.dll没有被指定在windows上运行   原因可能是压缩以后,.dll文件也被压缩导致windows无法识别,因此可以尝试选择执行opts1或opts2检查是否正常,若依然没有解决请尝试使用其他方法】

对于执行.exe文件一闪而过问题,可尝试在.py文件中加入语句
 

import os

***

os.system('pause')

对于可能出现failed to script xxx的错误原因就有太多啦,这里给出本人遇到的解决办法:可首先尝试cmd命令行启动该exe文件

方法如下:cd切换到该该项目目录dist下(exe程序父目录),然后输入.\’exe程序名‘,若正常执行,则应该能帮你排除一些问题,总之只能慢慢尝试啦

Microsoft Windows [版本 10.0.16299.431]
(c) 2017 Microsoft Corporation。保留所有权利。

C:\Users\SunChao>cd C:\Users\SunChao\Desktop\series\dist\

C:\Users\SunChao\Desktop\series\dist>.\test1
请输入字符串:hahah
您输入的字符串为:hahah
successful!
请按任意键继续. . .

C:\Users\SunChao\Desktop\series\dist>

同时生成的.spec文件也是非常重要的一部分内容,

具体的使用方法可查看官方文档:https://pyinstaller.readthedocs.io/en/stable/spec-files.html

There are four cases where it is useful to modify the spec file:

  • When you want to bundle data files with the app.
  • When you want to include run-time libraries (.dll or .so files) that PyInstaller does not know about from any other source.
  • When you want to add Python run-time options to the executable.
  • When you want to create a multiprogram bundle with merged common modules.

或许你的一些脚本中使用了import()等格式的导入,这是pyinstaller无法识别的包导入格式,或者在运行时操作sys.path的值等等。这些都是pyinstaller无法直接识别的。需要你手动做一些其他的工作帮助pyinstaller识别这些内容。

    通过向pyinstaller添加需要的文件或者路径
    编辑 你的文件名.spec文件来添加pystaller无法自动识别的内容(后续将详细介绍)
    写hook文件来提醒pyinstaller那些隐藏的import

# -*- mode: python -*-

block_cipher = None


a = Analysis(['test1.py'],
             pathex=['C:\\Users\\SunChao\\Desktop\\series'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='test1',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )
spec文件中主要包含4个class: Analysis, PYZ, EXE和COLLECT.

Analysis以py文件为输入,它会分析py文件的依赖模块,并生成相应的信息
PYZ是一个.pyz的压缩包,包含程序运行需要的所有依赖
EXE根据上面两项生成
COLLECT生成其他部分的输出文件夹,COLLECT也可以没有

修改spec文件

我们上面说过有时候PyInstaller自动生成的spec文件并不能满足我们的需求,最常见的情况就是我们的程序依赖我们本地的一些数据文件,这个时候就需要我们自己去编辑spec文件来添加数据文件了。
上面的spec文件解析中Analysis中的datas就是要添加到项目中的数据文件,我们可以编辑datas.


a = Analysis(
…
datas = [(‘you/source/file/path’,’file_name_in_project’),
(‘source/file2’, ‘file_name2’)] …
)

可以认为datas是一个List,每个元素是一个二元组。元组的第一个元素是你本地文件索引,第二个元素是拷贝到项目中之后的文件名字。除了上面那种写法,也可以将其提出来。

added_files = […]

a = Analysis(
…
datas = added_files,
…
)

 


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值