python文件(.py)转换为可执行文件(.exe)操作

1 篇文章 0 订阅

关于

Python学习笔记(15)-Python代码转换为exe可执行程序详解_熊猫大哥大-CSDN博客_python转化为exe

Python 将.py转换为.exe详解_tangdaxue43的博客-CSDN博客_py转换成exe

的一些小心得;

下载PyInstaller

pip install pyinstaller

结果如下:意思是已经装上了。 

下载pywin32

先看看python目录C:\Users\DELL\AppData\Local\Programs\Python\Python39\Scripts里有没有pywin32_postinstall.py,如果有直接在这个目录下执行:

python pywin32_postinstall.py -install

再看看目录里的内容,确定有没有需要的pyinstaller.exe,执行ls,结果如下图:

ls

可以把这个pyinstaller.exe复制出来到任意文件夹,直接用,保证pyinstaller.exe和带转换的.py同在一个文件夹即可。

开始转换

自己编写一个.py文件,其实txt文件就可以,把.txt改成.py重命名。和pyinstaller.exe放在一个文件夹里。

print('hello world!')

input()

执行(exe文件名前面的.\是执行的意思),注意文件名称。

.\pyinstaller.exe -F .\helloworld.py

PS D:\WORK> .\pyinstaller.exe -F .\helloworld.py
96 INFO: PyInstaller: 4.7
96 INFO: Python: 3.9.6
104 INFO: Platform: Windows-10-10.0.19044-SP0
104 INFO: wrote D:\WORK\helloworld.spec
108 INFO: UPX is not available.
113 INFO: Extending PYTHONPATH with paths
['D:\\WORK']
591 INFO: checking Analysis
591 INFO: Building Analysis because Analysis-00.toc is non existent
591 INFO: Initializing module dependency graph...
594 INFO: Caching module graph hooks...
616 INFO: Analyzing base_library.zip ...
3492 INFO: Processing pre-find module path hook distutils from 'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-distutils.py'.
3493 INFO: distutils: retargeting to non-venv dir 'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python39\\lib'
6888 INFO: Caching module dependency graph...
7023 INFO: running Analysis Analysis-00.toc
7036 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by C:\Users\DELL\AppData\Local\Programs\Python\Python39\python.exe
7107 INFO: Analyzing D:\WORK\Technology Support\From LiangHe\New OTA\ota\ComReader\helloworld.py
7112 INFO: Processing module hooks...
7112 INFO: Loading module hook 'hook-difflib.py' from 'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
7114 INFO: Loading module hook 'hook-distutils.py' from 'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
7114 INFO: Loading module hook 'hook-distutils.util.py' from 'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
7115 INFO: Loading module hook 'hook-encodings.py' from 'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
7196 INFO: Loading module hook 'hook-heapq.py' from 'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
7198 INFO: Loading module hook 'hook-lib2to3.py' from 'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
7262 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
7264 INFO: Loading module hook 'hook-pickle.py' from 'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
7265 INFO: Loading module hook 'hook-sysconfig.py' from 'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
7267 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
7268 INFO: Loading module hook 'hook-xml.py' from 'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
7368 INFO: Loading module hook 'hook-_tkinter.py' from 'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
7552 INFO: checking Tree
7552 INFO: Building Tree because Tree-00.toc is non existent
7554 INFO: Building Tree Tree-00.toc
7665 INFO: checking Tree
7665 INFO: Building Tree because Tree-01.toc is non existent
7666 INFO: Building Tree Tree-01.toc
7764 INFO: checking Tree
7764 INFO: Building Tree because Tree-02.toc is non existent
7764 INFO: Building Tree Tree-02.toc
7779 INFO: Looking for ctypes DLLs
7789 INFO: Analyzing run-time hooks ...
7790 INFO: Including run-time hook 'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_pkgutil.py'
7798 INFO: Including run-time hook 'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_multiprocessing.py'
7805 INFO: Including run-time hook 'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_inspect.py'
7814 INFO: Looking for dynamic libraries
8022 INFO: Looking for eggs
8022 INFO: Using Python library C:\Users\DELL\AppData\Local\Programs\Python\Python39\python39.dll
8023 INFO: Found binding redirects:
[]
8030 INFO: Warnings written to D:\WORK\build\helloworld\warn-helloworld.txt
8059 INFO: Graph cross-reference written to D:\WORK\build\helloworld\xref-helloworld.html
8068 INFO: checking PYZ
8068 INFO: Building PYZ because PYZ-00.toc is non existent
8069 INFO: Building PYZ (ZlibArchive) D:\WORK\build\helloworld\PYZ-00.pyz
8432 INFO: Building PYZ (ZlibArchive) D:\WORK\build\helloworld\PYZ-00.pyz completed successfully.
8444 INFO: checking PKG
8444 INFO: Building PKG because PKG-00.toc is non existent
8445 INFO: Building PKG (CArchive) helloworld.pkg
9684 INFO: Building PKG (CArchive) helloworld.pkg completed successfully.
9686 INFO: Bootloader C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
9686 INFO: checking EXE
9686 INFO: Building EXE because EXE-00.toc is non existent
9686 INFO: Building EXE from EXE-00.toc
9686 INFO: Copying bootloader EXE to D:\WORK\dist\helloworld.exe
9734 INFO: Copying icon to EXE
9735 INFO: Copying icons from ['C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\bootloader\\images\\icon-console.ico']
9780 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
9780 INFO: Writing RT_ICON 1 resource with 3752 bytes
9781 INFO: Writing RT_ICON 2 resource with 2216 bytes
9789 INFO: Writing RT_ICON 3 resource with 1384 bytes
9789 INFO: Writing RT_ICON 4 resource with 37019 bytes
9790 INFO: Writing RT_ICON 5 resource with 9640 bytes
9790 INFO: Writing RT_ICON 6 resource with 4264 bytes
9791 INFO: Writing RT_ICON 7 resource with 1128 bytes
9793 INFO: Copying 0 resources to EXE
9793 INFO: Emedding manifest in EXE
9794 INFO: Updating manifest in D:\WORK\dist\helloworld.exe
9839 INFO: Updating resource type 24 name 1 language 0
9841 INFO: Appending PKG archive to EXE
10839 INFO: Building EXE from EXE-00.toc completed successfully.

在dist文件夹里有exe文件:

 双击运行显示结果:

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python脚本转换为可执行的.exe文件可以提供更好的用户体验,因为用户可以直接双击文件来运行程序,而无需安装Python环境或运行Python脚本。以下是将Python脚本转换为可执行的.exe文件的详细步骤: 1. 安装pyinstaller 在命令行中使用以下命令安装pyinstaller: ``` pip install pyinstaller ``` 2. 编写Python脚本 在Python中编写您的脚本,并确保它能够正常运行。 3. 创建.spec文件 在命令行中进入您的Python脚本所在的目录,并使用以下命令创建.spec文件: ``` pyinstaller --name=your_program_name your_program.py ``` 其中,`--name`选项指定生成的可执行文件的名称,`your_program.py`是您的Python脚本文件的名称。 4. 修改.spec文件 在您的Python脚本所在的目录中,找到名为`your_program.spec`的文件,并打开它以编辑。在.spec文件中,您可以对生成的可执行文件进行更多的配置,例如添加依赖项,指定图标等。在spec文件中,在`Analysis`和`exe`部分添加以下代码: ``` a = Analysis(['your_program.py'], pathex=['your_script_path'], binaries=[], datas=[], hiddenimports=[], hookspath=[]) pyz = PYZ(a.pure, a.zipped_data, cipher=None) exe = EXE(pyz, a.scripts, a.binaries, a.zipfiles, a.datas, [], name='your_program_name', debug=False, bootloader_ignore_signals=False, strip=False, upx=True, upx_exclude=[], runtime_tmpdir=None, console=True, disable_windowed_traceback=False, target_arch=None, codesign_identity=None, entitlements_file=None ) ``` - `your_program.py`:您的Python脚本文件的名称。 - `your_script_path`:您的Python脚本所在的完整路径。 - `your_program_name`:生成的可执行文件的名称。 5. 生成可执行文件 在命令行中,进入您的Python脚本所在的目录,并使用以下命令生成可执行文件: ``` pyinstaller your_program.spec ``` 生成的可执行文件将位于`dist`文件夹中。 注意:生成的可执行文件需要在与Python版本和操作系统相同的计算机上运行。如果您的Python脚本中使用了第三方库,用户需要先安装这些库才能运行可执行文件。您可以使用`--onefile`选项生成单个的可执行文件,以便更方便地分发您的应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值