python编译成exe的方法

  介绍

这篇文章给大家分享的是有关python编译成exe的方法的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。

python程序都是脚本的方式,一般是在解析器里运行,如果要发布出去,需要提前安装解析器才可以运行,为了在Windows里方便发布,只要点击一个exe文件运行,并且打包所需要库文件,这样发布给用户使用就会更方便。

 python编译成exe的方法

<强> PyInstaller

<强> PyInstaller是一个十分有用的第三方库,可以用来打python包应用程序,打包完的程序就可以在没有安装python解释器的机器上运行了。

更多相关知识,可以参考这篇文章:《python如何编译成exe文件》

它能够在Windows、Linux、Mac OS X等操作系统下将python源文件打包,通过对源文件打包,python程序可以在没有安装python的环境中运行,也可以作为一个独立文件方便传递和管理。

PyInstaller支持python -3.7 2.7/3.4。可以在Windows、Mac OS X和Linux上使用,但是并不是跨平台的,而是说你要是希望打包成. exe文件,需要在Windows系统上运行PyInstaller进行打包工作。

<强>下面我们以Windows为例来进行程序的打包工作。

<>强安装

pip  install  PyInstaller   #,或者   python  -m  pip  install  pyinstaller

<强>使用

pyinstaller  -F  helloworld。py

其中,- f表示打包成单独的. exe文件,这时生成的. exe文件会比较大,而且运行速度回较慢。仅仅一个helloworld程序,生成的文件就5 mb大。

另外,使用,我还可以指定可执行文件的图标;

- w表示去掉控制台窗口,这在GUI界面时非常有用。不过如果是命令行程序的话那就把这个选项删除吧!

<强> PyInstaller会对脚本进行解析,并做出如下动作:

1,在脚本目录生成helloworld。规范文件;

2,创建一个构建目录;

3,写入一些日志文件和中间流程文件到构建目录;

4,创建dist目录;

5,生成可执行文件到dist目录;

<强>执行流程:

,美元pyinstaller  -F  helloworld.py   838年,信息:PyInstaller: 3.4   839年,信息:Python: 3.4.3   841年,信息:平台:Windows-8-6.2.9200   842年,信息:wrote  d: \ \ \ pyinstaller \ helloworld.spec Python代码   858年,信息:UPX  is  not 可用。   885年,信息:Extending  PYTHONPATH  with 路径   [& # 39;d: \ \ \ \ Python代码\ \ pyinstaller& # 39;,, & # 39; d: \ \ \ \ Python代码\ \ pyinstaller& # 39;】   886年,信息:checking 分析   887年,信息:Building  Analysis  because 分析- 00. toc  is  non 存在的   888年,信息:Initializing  module  dependency 图…   890年,信息:Initializing  module  graph 钩子……   899年,信息:Analyzing  base_library.zip …   6225年,信息:Processing  pre-find  module  path  hook , distutils   11387,信息:running  Analysis 分析- 00. - toc   12012,信息:Caching  module 钩子……   12022,信息:Analyzing  d: \ \ \ pyinstaller \ helloworld.py Python代码   12027,信息:Loading  module 钩子……   12028,信息:Loading  module  hook “hook-encodings.py"…   12395,信息:Loading  module  hook “hook-xml.py"…   13507,信息:Loading  module  hook “hook-pydoc.py"…   13508,信息:Loading  module  hook “hook-distutils.py"…   13606,信息:Looking  for  ctypes  dll   13662,信息:Analyzing  run-time  hooks …   13677,信息:Looking  for  dynamic 库   13894,信息:Looking  for 鸡蛋   13895,信息:Using  Python  library  C: \ WINDOWS \ system32系统\ python34.dll   13895,信息:Found  binding 重定向:   []   13915,信息:Warnings  written 用Python代码d: \ \ \ pyinstaller \ \ helloworld \ warn-helloworld.txt构建   14035,信息:Graph  cross-reference  written 用Python代码d: \ \ \ pyinstaller \ \ helloworld \ xref-helloworld.html构建   14287,信息:checking  PYZ   14287,信息:Building  PYZ  because  pyz - 00. - toc  is  non 存在的   14288,信息:Building  PYZ  (ZlibArchive), d: \ Python代码\ \ pyinstaller \制造\ helloworld \ pyz pyz——00.   15836,信息:Building  PYZ  (ZlibArchive), d: \ Python代码\ \ pyinstaller \制造\ helloworld \ pyz - 00. - PYZ  completed 成功。   15883,信息:checking 包裹   15884,信息:Building  PKG  because  pkg - 00. - toc  is  non 存在的   15884,信息:Building  PKG  (CArchive), pkg - 00.包裹   18528,信息:Building  PKG  (CArchive), pkg - 00. - PKG  completed 成功。   18536,信息:Bootloader  D: \程序\ Python34 \ lib \网站\ PyInstaller \引导装载程序\ windows - 64位\ run.exe   18537,信息:checking  EXE   18537,信息:Building  EXE  because  exe - 00. - toc  is  non 存在的   18538,信息:Building  EXE 得到exe - 00. - toc   18538,信息:Appending  archive 用Python代码EXE  d: \ \ \ pyinstaller \ dist \ helloworld.exe   18548,信息:Building  EXE 得到exe - 00. - toc  completed 成功。null

python编译成exe的方法