python如何编译成exe

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

图片[1]-python如何编译成exe-uusu优素-乐高,模型,3d打印,编程

PyInstaller

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

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

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

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

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

安装

pipinstallpyinstaller
#或者
python-mpipinstallpyinstaller

使用

pyinstaller-Fhelloworld.py

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

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

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

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

1、在脚本目录生成 helloworld.spec 文件;

2、创建一个 build 目录;

3、写入一些日志文件和中间流程文件到 build 目录;

4、创建 dist 目录;

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

执行流程:

$pyinstaller-Fhelloworld.py
838INFO:PyInstaller:3.4
839INFO:Python:3.4.3
841INFO:Platform:Windows-8-6.2.9200
842INFO:wroted:\code\Python\pyinstaller\helloworld.spec
858INFO:UPXisnotavailable.
885INFO:ExtendingPYTHONPATHwithpaths
['d:\\code\\Python\\pyinstaller','d:\\code\\Python\\pyinstaller']
886INFO:checkingAnalysis
887INFO:BuildingAnalysisbecauseAnalysis-00.tocisnonexistent
888INFO:Initializingmoduledependencygraph...
890INFO:Initializingmodulegraphhooks...
899INFO:Analyzingbase_library.zip...
6225INFO:Processingpre-findmodulepathhookdistutils
11387INFO:runningAnalysisAnalysis-00.toc
12012INFO:Cachingmodulehooks...
12022INFO:Analyzingd:\code\Python\pyinstaller\helloworld.py
12027INFO:Loadingmodulehooks...
12028INFO:Loadingmodulehook"hook-encodings.py"...
12395INFO:Loadingmodulehook"hook-xml.py"...
13507INFO:Loadingmodulehook"hook-pydoc.py"...
13508INFO:Loadingmodulehook"hook-distutils.py"...
13606INFO:LookingforctypesDLLs
13662INFO:Analyzingrun-timehooks...
13677INFO:Lookingfordynamiclibraries
13894INFO:Lookingforeggs
13895INFO:UsingPythonlibraryC:\WINDOWS\system32\python34.dll
13895INFO:Foundbindingredirects:
[]
13915INFO:Warningswrittentod:\code\Python\pyinstaller\build\helloworld\warn-helloworld.txt
14035INFO:Graphcross-referencewrittentod:\code\Python\pyinstaller\build\helloworld\xref-helloworld.html
14287INFO:checkingPYZ
14287INFO:BuildingPYZbecausePYZ-00.tocisnonexistent
14288INFO:BuildingPYZ(ZlibArchive)d:\code\Python\pyinstaller\build\helloworld\PYZ-00.pyz
15836INFO:BuildingPYZ(ZlibArchive)d:\code\Python\pyinstaller\build\helloworld\PYZ-00.pyzcompletedsuccessfully.
15883INFO:checkingPKG
15884INFO:BuildingPKGbecausePKG-00.tocisnonexistent
15884INFO:BuildingPKG(CArchive)PKG-00.pkg
18528INFO:BuildingPKG(CArchive)PKG-00.pkgcompletedsuccessfully.
18536INFO:BootloaderD:\program\Python34\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
18537INFO:checkingEXE
18537INFO:BuildingEXEbecauseEXE-00.tocisnonexistent
18538INFO:BuildingEXEfromEXE-00.toc
18538INFO:AppendingarchivetoEXEd:\code\Python\pyinstaller\dist\helloworld.exe
18548INFO:BuildingEXEfromEXE-00.toccompletedsuccessfully.

生成文件:

原文来自:https://www.py.cn
© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片快捷回复

    暂无评论内容