pyinstaller:只需一步,即可将.py文件转换为.exe文件

虽然计算机专业的同学们一般用不上这个,毕竟电脑上应该都有解释、运行python的相关IDE。但是有时难免需要转换,则只需要一步操作,即可得到.exe文件。

工具:

  1. cmd命令窗口

  1. pip

  1. pyinstaller

步骤:

下载pyinstaller:

Win + R打开cmd命令窗口,在窗口中输入命令(俺之前已经下好了):

图一

注意:利用pip下载库或者包,必须先有pip,否则是无法使用这条命令安装的。

安装好pyinstaller后,如果.py文件在C盘下,则直接利用cmd命令:cd 目标程序所在文件夹地址 进入目标地址。

假如,我需要转换的1.py文件不在C盘下,而在D盘下,比如1.py文件在目录D:\Python_Practice下:

图二

那么不能直接使用命令:cd 目标程序所在文件夹地址 进入目标地址。而是需要先退出C盘。

那么在cmd命令行中写:

cd/                            #回到根目录
d:                             #进入D盘
cd D:\Python_Practice          #进入目标文件夹

如图所示(接图一):

图三

此时利用命令:

pyinstaller -F 文件名.py

即可转化(这里我不截图了,因为太多无用行,就直接复制cmd里的内容了,下面的内容接图三,从第二行开始是电脑自己生成的):

D:\Python_Practice>pyinstaller -F 1.py    #从第二行开始是电脑自己生成的
270 INFO: PyInstaller: 5.8.0
270 INFO: Python: 3.10.8
278 INFO: Platform: Windows-10-10.0.22621-SP0
279 INFO: wrote D:\Python_Practice\1.spec
280 INFO: UPX is not available.
282 INFO: Extending PYTHONPATH with paths
['D:\\Python_Practice']
483 INFO: checking Analysis
484 INFO: Building Analysis because Analysis-00.toc is non existent
484 INFO: Initializing module dependency graph...
485 INFO: Caching module graph hooks...
497 INFO: Analyzing base_library.zip ...
1495 INFO: Loading module hook 'hook-heapq.py' from 'C:\\Users\\……\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
1552 INFO: Loading module hook 'hook-encodings.py' from 'C:\\Users\\……\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
2793 INFO: Loading module hook 'hook-pickle.py' from 'C:\\Users\\……\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
4570 INFO: Caching module dependency graph...
4643 INFO: running Analysis Analysis-00.toc
4649 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by C:\Users\……\AppData\Local\Programs\Python\Python310\python.exe
4687 INFO: Analyzing D:\Python_Practice\1.py
4739 INFO: Processing module hooks...
4749 INFO: Looking for ctypes DLLs
4752 INFO: Analyzing run-time hooks ...
4754 INFO: Including run-time hook 'C:\\Users\\……\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_inspect.py'
4761 INFO: Looking for dynamic libraries
210 INFO: Extra DLL search directories (AddDllDirectory): []
211 INFO: Extra DLL search directories (PATH): ['C:\\windows\\system32', 'C:\\windows', 'C:\\windows\\System32\\Wbem', 'C:\\windows\\System32\\WindowsPowerShell\\v1.0\\', 'C:\\windows\\System32\\OpenSSH\\', 'C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common', 'C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR', 'C:\\WINDOWS\\system32', 'C:\\WINDOWS', 'C:\\WINDOWS\\System32\\Wbem', 'C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\', 'C:\\WINDOWS\\System32\\OpenSSH\\', 'C:\\MyPrograms\\Git\\cmd', 'C:\\Users\\……\\AppData\\Local\\Programs\\Python\\Python310\\Scripts\\', 'C:\\Users\\……\\AppData\\Local\\Programs\\Python\\Python310\\', 'C:\\Users\\……\\AppData\\Local\\Microsoft\\WindowsApps', 'C:\\MyPrograms\\JetBrains\\PyCharm 2022.2.2\\bin']
5253 INFO: Looking for eggs
5253 INFO: Using Python library C:\Users\……\AppData\Local\Programs\Python\Python310\python310.dll
5253 INFO: Found binding redirects:
[]
5255 INFO: Warnings written to D:\Python_Practice\build\1\warn-1.txt
5269 INFO: Graph cross-reference written to D:\Python_Practice\build\1\xref-1.html
5304 INFO: checking PYZ
5304 INFO: Building PYZ because PYZ-00.toc is non existent
5304 INFO: Building PYZ (ZlibArchive) D:\Python_Practice\build\1\PYZ-00.pyz
5468 INFO: Building PYZ (ZlibArchive) D:\Python_Practice\build\1\PYZ-00.pyz completed successfully.
5472 INFO: checking PKG
5472 INFO: Building PKG because PKG-00.toc is non existent
5472 INFO: Building PKG (CArchive) 1.pkg
6709 INFO: Building PKG (CArchive) 1.pkg completed successfully.
6710 INFO: Bootloader C:\Users\……\AppData\Local\Programs\Python\Python310\lib\site-packages\PyInstaller\bootloader\Windows-64bit-intel\run.exe
6710 INFO: checking EXE
6713 INFO: Building EXE because EXE-00.toc is non existent
6714 INFO: Building EXE from EXE-00.toc
6714 INFO: Copying bootloader EXE to D:\Python_Practice\dist\1.exe.notanexecutable
6726 INFO: Copying icon to EXE
6726 INFO: Copying icons from ['C:\\Users\\……\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\bootloader\\images\\icon-console.ico']
6730 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
6738 INFO: Writing RT_ICON 1 resource with 3752 bytes
6738 INFO: Writing RT_ICON 2 resource with 2216 bytes
6738 INFO: Writing RT_ICON 3 resource with 1384 bytes
6739 INFO: Writing RT_ICON 4 resource with 37019 bytes
6745 INFO: Writing RT_ICON 5 resource with 9640 bytes
6745 INFO: Writing RT_ICON 6 resource with 4264 bytes
6745 INFO: Writing RT_ICON 7 resource with 1128 bytes
6747 INFO: Copying 0 resources to EXE
6754 INFO: Embedding manifest in EXE
6754 INFO: Updating manifest in D:\Python_Practice\dist\1.exe.notanexecutable
6755 INFO: Updating resource type 24 name 1 language 0
6763 INFO: Appending PKG archive to EXE
6773 INFO: Fixing EXE headers
6837 INFO: Building EXE from EXE-00.toc completed successfully.
D:\Python_Practice>

这时,打开.py所在的文件夹:

发现有个dist文件夹,打开它:

Well done。

猜你喜欢

转载自blog.csdn.net/qq_43674552/article/details/129335615