用PyInstaller把Python代码打包成单个独立的exe可执行文件

本文介绍了如何使用PyInstaller将复杂的Python脚本BlogsToWordpress打包成独立的exe可执行文件,包括遇到的问题及解决方法,如设置搜索路径、添加图标,并强调检查warn文件以确保所有依赖项都被正确处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前就想要把自己的BlogsToWordpress打开成exe了。一直没去弄。

又看到有人提到python打开成exe的问题。

所以打算现在就去试试。

注:此处之所有选用BlogsToWordpress,是因为此python脚本够复杂,依赖的模块够多。

如果这个都搞定了,那么其他单个的python文件,和小python项目的打包,就更不成问题了。


1.先去找找,目前主流有哪几种方法。

找到几个名字

cx_Freeze

PyInstaller

py2exe

2.关于py2exe和PyInstaller的两者官网:

http://www.py2exe.org/index.cgi/FAQ

http://www.pyinstaller.org/

比较了一下,发现貌似后者更好用。

因为貌似py2exe还需要另外的其他库文件啊啥的,太琐碎。

而PyInstaller:

  • 可以只是生成单独的可执行程序
  • 且支持的版本也多:2.3到2.7都支持。以及x64也支持
  • 也可以自定义图标

所以先去试试PyInstaller。

3.从主页

http://www.pyinstaller.org/

中下载对应的zip包:

https://github.com/downloads/pyinstaller/pyinstaller/pyinstaller-2.0.zip

得到3M+的pyinstaller-2.0.zip,解压,打开cmd,进入对应目录,去安装,结果说不支持:

?
1
2
3
D:\tmp\dev_tools\python\to_exe\PyInstaller\pyinstaller-2.0>setup.py install
  
setup.py is not yet supposed to work. Please Use PyInstaller without installation.

所以算了,还是参考官网文档,一点点折腾吧。

4.参考在线文档:

http://www.pyinstaller.org/export/v2.0/project/doc/Manual.html?format=raw

结果说需要PyWin32,所以得先去装这个。

5.后来才发现,原来之前已经安装过了:

【已解决】Python中出错:ImportError: No module named win32com.client

但是要注意的是,import不是pywin32,而是win32com:

?
1
2
3
4
5
6
7
8
Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v .1500 64 bit (AMD64)] on win32
Type "help" , "copyright" , "credits" or "license" for more information.
>>> import pywin32
Traceback (most recent call last):
   File "<stdin>" , line 1, in <module>
ImportError: No module named pywin32
>>> import win32com
>>>

6.继续参考:

Installing PyInstaller

发现,解压后,就算是安装好了。

7.在一个文件夹中,准备好自己的Python程序:

D:\tmp\tmp_dev_root\python\tutorial_summary\make_exe\BlogsToWordpress

以备后用。

8.去到pyinstaller.py所在目录,去运行:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
D:\tmp\tmp_dev_root\python\tutorial_summary\make_exe\pyinstaller-2.0>pyinstaller.py ..\BlogsToWordpress\BlogsToWordpress.py
232 INFO: wrote D:\tmp\tmp_dev_root\python\tutorial_summary\make_exe\pyinstaller-2.0\BlogsToWordpress\BlogsToWordpress.spec
250 INFO: Testing for ability to set icons, version resources...
269 INFO: ... resource update available
272 INFO: UPX is not available.
1437 INFO: checking Analysis
1437 INFO: building Analysis because out00-Analysis.toc non existent
1437 INFO: running Analysis out00-Analysis.toc
1439 INFO: Adding Microsoft.VC90.CRT to dependent assemblies of final executable
3074 INFO: Searching for assembly amd64_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.21022.8_none ...
3075 INFO: Found manifest C:\Windows\WinSxS\Manifests\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.21022.8_none_750b37ff97f4f68b.manifest
3081 INFO: Searching for file msvcr90.dll
3081 INFO: Found file C:\Windows\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.21022.8_none_750b37ff97f4f68b\msvcr90.dll
3081 INFO: Searching for file msvcp90.dll
3082 INFO: Found file C:\Windows\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.21022.8_none_750b37ff97f4f68b\msvcp90.dll
3082 INFO: Searching for file msvcm90.dll
3082 INFO: Found file C:\Windows\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.21022.8_none_750b37ff97f4f68b\msvcm90.dll
3292 INFO: Analyzing D:\tmp\tmp_dev_root\python\tutorial_summary\make_exe\pyinstaller-2.0\support\_pyi_bootstrap.py
4048 INFO: Analyzing D:\tmp\tmp_dev_root\python\tutorial_summary\make_exe\pyinstaller-2.0\PyInstaller\loader\archive.py
4101 INFO: Analyzing D:\tmp\tmp_dev_root\python\tutorial_summary\make_exe\pyinstaller-2.0\PyInstaller\loader\carchive.py
4159 INFO: Analyzing D:\tmp\tmp_dev_root\python\tutorial_summary\make_exe\pyinstaller-2.0\PyInstaller\loader\iu.py
4176 INFO: Analyzing ..\BlogsToWordpress\BlogsToWordpress.py
4880 INFO: Hidden import 'encodings' has been found otherwise
4881 INFO: Looking for run- time hooks
4881 INFO: Analyzing rthook D:\tmp\tmp_dev_root\python\tutorial_summary\make_exe\pyinstaller-2.0\support /rthooks/pyi_rth_encodings .py
5523 INFO: Warnings written to D:\tmp\tmp_dev_root\python\tutorial_summary\make_exe\pyinstaller-2.0\BlogsToWordpress\build\pyi.win32\BlogsToWordpress\warnBlogsToWordpress.txt
5528 INFO: checking PYZ
5529 INFO: rebuilding out00-PYZ.toc because out00-PYZ.pyz is missing
5529 INFO: building PYZ out00-PYZ.toc
6225 INFO: checking PKG
6226 INFO: rebuilding out00-PKG.toc because out00-PKG.pkg is missing
6226 INFO: building PKG out00-PKG.pkg
6246 INFO: checking EXE
6246 INFO: rebuilding out00-EXE.toc because BlogsToWordpress.exe missing
6248 INFO: building EXE from out00-EXE.toc
6256 INFO: Appending archive to EXE D:\tmp\tmp_dev_root\python\tutorial_summary\make_exe\pyinstaller-2.0\BlogsToWordpress\build\pyi.win32\BlogsToWordpress\BlogsToWordpress.exe
6259 INFO: checking COLLECT
6259 INFO: building COLLECT out00-COLLECT.toc
  
D:\tmp\tmp_dev_root\python\tutorial_summary\make_exe\pyinstaller-2.0>

可以看到:

当前pyinstaller下面,的确生成了对应的BlogsToWordpress文件夹:

generated project subdir

其下,也有对应的:

build dist spec

都已经生成了对应的exe了:

has generated exe

dist下面,有对应的,exe,pyd,dll等等:

dist include dll and exe and pyd

汗,效率也忒高了,我还以为,需要折腾半天才可以呢,囧。。。。

9.先去试试生成的exe,是否满足我们的需求,是否可以独立运行。

不过运行之前,还是去先研究一下对应的参数吧。

看到了:

What to generate:

-F, --onefile

create a single file deployment

-D, --onedir

create a single directory deployment (default)

-o DIR, --out=DIR

create the spec file in directory. If not specified, and the current directory is Installer’s root directory, an output subdirectory will be created. Otherwise the current directory is used.

-n NAME, --name=NAME

optional name to assign to the project (from which the spec file name is generated). If omitted, the basename of the (first) script is used.

 

很明显,默认用的是-D,所以生成的是带目录的,现在重新去执行一次,使用-F生成单一的文件。

10.删掉旧的。重新运行:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
D:\tmp\tmp_dev_root\python\tutorial_summary\make_exe\pyinstaller-2.0>pyinstaller.py -F ..\BlogsToWordpress\BlogsToWordpress.py
39 INFO: wrote D:\tmp\tmp_dev_root\python\tutorial_summary\make_exe\pyinstaller-2.0\BlogsToWordpress\BlogsToWordpress.spec
55 INFO: Testing for ability to set icons, version resources...
62 INFO: ... resource update available
63 INFO: UPX is not available.
848 INFO: checking Analysis
848 INFO: building Analysis because out00-Analysis.toc non existent
849 INFO: running Analysis out00-Analysis.toc
851 INFO: Adding Microsoft.VC90.CRT to dependent assemblies of final executable
905 INFO: Searching for assembly amd64_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.21022.8_none ...
907 INFO: Found manifest C:\Windows\WinSxS\Manifests\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.21022.8_none_750b37ff97f4f68b.manifest
908 INFO: Searching for file msvcr90.dll
908 INFO: Found file C:\Windows\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.21022.8_none_750b37ff97f4f68b\msvcr90.dll
911 INFO: Searching for file msvcp90.dll
911 INFO: Found file C:\Windows\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.21022.8_none_750b37ff97f4f68b\msvcp90.dll
912 INFO: Searching for file msvcm90.dll
914 INFO: Found file C:\Windows\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.21022.8_none_750b37ff97f4f68b\msvcm90.dll
987 INFO: Analyzing D:\tmp\tmp_dev_root\python\tutorial_summary\make_exe\pyinstaller-2.0\support\_pyi_bootstrap.py
1639 INFO: Analyzing D:\tmp\tmp_dev_root\python\tutorial_summary\make_exe\pyinstaller-2.0\PyInstaller\loader\archive.py
1694 INFO: Analyzing D:\tmp\tmp_dev_root\python\tutorial_summary\make_exe\pyinstaller-2.0\PyInstaller\loader\carchive.py
1750 INFO: Analyzing D:\tmp\tmp_dev_root\python\tutorial_summary\make_exe\pyinstaller-2.0\PyInstaller\loader\iu.py
1766 INFO: Analyzing ..\BlogsToWordpress\BlogsToWordpress.py
2246 INFO: Hidden import 'encodings' has been found otherwise
2247 INFO: Looking for run- time hooks
2249 INFO: Analyzing rthook D:\tmp\tmp_dev_root\python\tutorial_summary\make_exe\pyinstaller-2.0\support /rthooks/pyi_rth_encodings .py
2608 INFO: Warnings written to D:\tmp\tmp_dev_root\python\tutorial_summary\make_exe\pyinstaller-2.0\BlogsToWordpress\build\pyi.win32\BlogsToWordpress\warnBlogsToWordpress.txt
2611 INFO: checking PYZ
2612 INFO: rebuilding out00-PYZ.toc because out00-PYZ.pyz is missing
2613 INFO: building PYZ out00-PYZ.toc
3290 INFO: checking PKG
3290 INFO: rebuilding out00-PKG.toc because out00-PKG.pkg is missing
3292 INFO: building PKG out00-PKG.pkg
4784 INFO: checking EXE
4784 INFO: rebuilding out00-EXE.toc because BlogsToWordpress.exe missing
4786 INFO: building EXE from out00-EXE.toc
4793 INFO: Appending archive to EXE D:\tmp\tmp_dev_root\python\tutorial_summary\make_exe\pyinstaller-2.0\BlogsToWordpress\dist\BlogsToWordpress.exe

去看看结果。

pyinstaller-2.0\BlogsToWordpress\build\pyi.win32\BlogsToWordpress

下面包含很多toc,pkg,pyz等文件:

<think>嗯,用户想知道怎么用PyInstallerPython项目打包Windows上的exe文件。首先,我需要回忆一下PyInstaller的基本用法。记得PyInstaller是一个常用的工具,可以把Python脚本转换独立可执行文件,这样用户不需要安装Python环境就能运行程序。不过,用户可能对这个过程不太熟悉,所以得一步步解释清楚。 首先,应该告诉用户安装PyInstaller的方法。通常是用pip安装,所以需要写pip install pyinstaller。不过可能用户已经安装了,或者需要升级到最新版本,这时候可能需要建议他们用--upgrade选项。 接下来,打包的基本命令。最简单的例子是pyinstaller --onefile main.py,这样会生单个exe文件。但用户可能有自己的项目结构,比如有多个文件或者依赖资源。这时候需要解释如何处理这些情况,比如使用--add-data选项来包含图片、数据文件等资源。另外,如果项目有隐藏的依赖,可能需要用--hidden-import来指定。 还有图标的问题,用户可能想给exe文件换个图标,这时候要用--icon参数,指定.ico文件。然后,生exe文件在dist目录下,需要提醒用户测试是否能在其他机器上运行。 另外,用户可能遇到路径问题,比如程序中用到了相对路径。这时候要提醒他们使用os.path.dirname(__file__)来获取当前脚本的路径,避免打包后路径错误。比如引用中的例子,用os.path.join来处理资源路径,这样打包后资源文件才能正确加载。 还有,可能需要提到一些常见问题,比如杀毒软件误报,或者打包后的文件大小较大。这时候可以建议使用--noconsole来隐藏命令行窗口,或者用UPX压缩来减小体积。 最后,生相关问题的时候,要涵盖用户可能进一步想知道的内容,比如处理依赖库、路径问题、减小体积、跨平台打包等。这样用户如果有其他疑问,也能找到答案。 现在整理一下步骤,确保每个环节都清楚,并且引用相关的参考资料。比如在路径处理部分,可以引用用户提供的引用3中的例子,说明如何正确使用相对路径。这样回答会更可靠,用户也能参考到具体的例子。</think>### 分步指南:使用PyInstaller打包Python项目为Windows可执行文件 #### 1. **安装PyInstaller** 通过pip安装PyInstaller(需提前安装Python环境): ```bash pip install pyinstaller ``` 若需升级到最新版本: ```bash pip install --upgrade pyinstaller ``` #### 2. **基础打包命令** - **单文件打包**:将主程序`main.py`打包单个`.exe`文件,生到`dist`目录: ```bash pyinstaller --onefile main.py ``` 生的`main.exe`路径为`./dist/main.exe`。 - **带图标的打包**:通过`--icon`指定`.ico`格式图标: ```bash pyinstaller --onefile --icon=app.ico main.py ``` #### 3. **处理复杂项目** - **添加数据文件**:若项目依赖图片、配置文件等资源,需使用`--add-data`参数,格式为`源路径:目标路径`: ```bash pyinstaller --onefile --add-data "assets/*.png;assets" main.py ``` 打包后资源文件会被复制到与可执行文件同级的`assets`目录下。 - **处理隐藏依赖**:某些动态导入的库需显式声明,例如`--hidden-import pandas`: ```bash pyinstaller --onefile --hidden-import pandas main.py ``` #### 4. **路径规范化** 在代码中**避免硬编码路径**,改用动态路径获取(参考引用[^3]): ```python import os # 获取当前脚本所在目录 base_dir = os.path.dirname(__file__) # 组合资源路径 icon_path = os.path.join(base_dir, "assets/icon.png") ``` #### 5. **测试与分发** - 在`dist`目录中找到生的`.exe`文件,双击运行测试。 - 若需分发,需将`dist`目录整体压缩(包含所有依赖文件)。 --- ### 常见问题与优化 - **体积过大**:使用UPX压缩(需[下载UPX](https://upx.github.io/)并指定路径): ```bash pyinstaller --onefile --upx-dir=/path/to/upx main.py ``` - **隐藏控制台窗口**(适用于GUI程序): ```bash pyinstaller --noconsole --onefile main.py ``` --- ### 引用说明 - 关于路径规范化的实现方法参考了引用的动态路径处理示例。 - 打包工具的核心原理与Python环境依赖关系可参考引用[^1]和[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值