知行编程网知行编程网  2022-08-14 09:30 知行编程网 隐藏边栏  12 
文章评分 0 次,平均分 0.0
导语: 本文主要介绍了关于python如何编译成exe的相关知识,包括python的编译器在哪里打开,以及python代码变成运行程序这些编程知识,希望对大家有参考作用。

Python 程序都是脚本,一般在解析器中运行。如果要发布,需要提前安装解析器才能运行。为了方便在Windows中发布,只需要点击一个EXE文件即可运行,并打包需要的库文件。 ,发布给用户会更方便。

如何将python编译成exe


PyInstaller

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

更多相关知识,可以参考这篇文章:《

它可以在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 为例来进行程序的打包工作。


安装

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


使用

pyinstaller -F helloworld.py

其中,-F表示打包成单独的.exe文件,生成的.exe文件会比较大,运行速度也会比较慢。只是一个helloworld程序,生成的文件有5MB大。

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

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


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

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

2、创建一个 build 目录;

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

4、创建 dist 目录;

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


执行流程:

$ pyinstaller -F helloworld.py
838 INFO: PyInstaller: 3.4
839 INFO: Python: 3.4.3
841 INFO: Platform: Windows-8-6.2.9200
842 INFO: wrote d:\code\Python\pyinstaller\helloworld.spec
858 INFO: UPX is not available.
885 INFO: Extending PYTHONPATH with paths
['d:\\code\\Python\\pyinstaller', 'd:\\code\\Python\\pyinstaller']
886 INFO: checking Analysis
887 INFO: Building Analysis because Analysis-00.toc is non existent
888 INFO: Initializing module dependency graph...
890 INFO: Initializing module graph hooks...
899 INFO: Analyzing base_library.zip ...
6225 INFO: Processing pre-find module path hook   distutils
11387 INFO: running Analysis Analysis-00.toc
12012 INFO: Caching module hooks...
12022 INFO: Analyzing d:\code\Python\pyinstaller\helloworld.py
12027 INFO: Loading module hooks...
12028 INFO: Loading module hook "hook-encodings.py"...
12395 INFO: Loading module hook "hook-xml.py"...
13507 INFO: Loading module hook "hook-pydoc.py"...
13508 INFO: Loading module hook "hook-distutils.py"...
13606 INFO: Looking for ctypes DLLs
13662 INFO: Analyzing run-time hooks ...
13677 INFO: Looking for dynamic libraries
13894 INFO: Looking for eggs
13895 INFO: Using Python library C:\WINDOWS\system32\python34.dll
13895 INFO: Found binding redirects:
[]
13915 INFO: Warnings written to d:\code\Python\pyinstaller\build\helloworld\warn-helloworld.txt
14035 INFO: Graph cross-reference written to d:\code\Python\pyinstaller\build\helloworld\xref-helloworld.html
14287 INFO: checking PYZ
14287 INFO: Building PYZ because PYZ-00.toc is non existent
14288 INFO: Building PYZ (ZlibArchive) d:\code\Python\pyinstaller\build\helloworld\PYZ-00.pyz
15836 INFO: Building PYZ (ZlibArchive) d:\code\Python\pyinstaller\build\helloworld\PYZ-00.pyz completed successfully.
15883 INFO: checking PKG
15884 INFO: Building PKG because PKG-00.toc is non existent
15884 INFO: Building PKG (CArchive) PKG-00.pkg
18528 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
18536 INFO: Bootloader D:\program\Python34\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
18537 INFO: checking EXE
18537 INFO: Building EXE because EXE-00.toc is non existent
18538 INFO: Building EXE from EXE-00.toc
18538 INFO: Appending archive to EXE d:\code\Python\pyinstaller\dist\helloworld.exe
18548 INFO: Building EXE from EXE-00.toc completed successfully.

生成文件:

如何将python编译成exe

如何将python编译成exe

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写
扫一扫二维码分享