windows下使用pyinstaller把python文件打包成exe可执行文件
使用pyinstaller打包有个好处就是所有依赖都打包进去了,可以随意把文件移动到别的电脑上使用
安装
pip install pyinstaller
新建一个demo.py文件
#!/usr/bin/python3 # -*- coding: utf-8 -*- print("hello world")
打开cmd或powershell
PS D:\build> pyinstaller.exe -F -c .\demo.py 150 INFO: PyInstaller: 3.3.1 150 INFO: Python: 3.6.4 151 INFO: Platform: Windows-10-10.0.16299-SP0 153 INFO: wrote D:\build\demo.spec 156 INFO: UPX is not available. 162 INFO: Extending PYTHONPATH with paths ['D:\\build', 'D:\\build'] 162 INFO: checking Analysis 163 INFO: Building Analysis because out00-Analysis.toc is non existent 163 INFO: Initializing module dependency graph... 167 INFO: Initializing module graph hooks... 172 INFO: Analyzing base_library.zip ... 3330 INFO: running Analysis out00-Analysis.toc 3334 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable required by c:\users\downtown\appdata\local\programs\python\python36\python.exe 3434 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python.exe 3522 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python.exe 3611 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python.exe 3701 WARNING: lib not found: api-ms-win-crt-locale-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python.exe 3791 WARNING: lib not found: api-ms-win-crt-math-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python.exe 4004 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll 4106 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll 4195 WARNING: lib not found: api-ms-win-crt-environment-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll 4306 WARNING: lib not found: api-ms-win-crt-convert-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll 4401 WARNING: lib not found: api-ms-win-crt-locale-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll 4492 WARNING: lib not found: api-ms-win-crt-time-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll 4581 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll 4671 WARNING: lib not found: api-ms-win-crt-process-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll 4765 WARNING: lib not found: api-ms-win-crt-conio-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll 4857 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll 4946 WARNING: lib not found: api-ms-win-crt-math-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll 5038 WARNING: lib not found: api-ms-win-crt-filesystem-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll 5139 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\VCRUNTIME140.dll 5231 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\VCRUNTIME140.dll 5324 WARNING: lib not found: api-ms-win-crt-convert-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\VCRUNTIME140.dll 5416 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\VCRUNTIME140.dll 5507 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\VCRUNTIME140.dll 5520 INFO: Caching module hooks... 5547 INFO: Analyzing D:\build\demo.py 5551 INFO: Loading module hooks... 5551 INFO: Loading module hook "hook-encodings.py"... 5695 INFO: Loading module hook "hook-pydoc.py"... 5705 INFO: Loading module hook "hook-xml.py"... 5970 INFO: Looking for ctypes DLLs 5970 INFO: Analyzing run-time hooks ... 5979 INFO: Looking for dynamic libraries 6107 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_ssl.pyd 6208 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_ssl.pyd 6303 WARNING: lib not found: api-ms-win-crt-utility-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_ssl.pyd 6412 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_ssl.pyd 6505 WARNING: lib not found: api-ms-win-crt-environment-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_ssl.pyd 6595 WARNING: lib not found: api-ms-win-crt-convert-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_ssl.pyd 6686 WARNING: lib not found: api-ms-win-crt-time-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_ssl.pyd 6783 WARNING: lib not found: api-ms-win-crt-filesystem-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_ssl.pyd 6876 WARNING: lib not found: api-ms-win-crt-conio-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_ssl.pyd 6976 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_ssl.pyd 7088 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\unicodedata.pyd 7180 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\unicodedata.pyd 7271 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\unicodedata.pyd 7392 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\pyexpat.pyd 7486 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\pyexpat.pyd 7577 WARNING: lib not found: api-ms-win-crt-environment-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\pyexpat.pyd 7668 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\pyexpat.pyd 7761 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\pyexpat.pyd 7874 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_hashlib.pyd 7969 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_hashlib.pyd 8062 WARNING: lib not found: api-ms-win-crt-utility-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_hashlib.pyd 8166 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_hashlib.pyd 8255 WARNING: lib not found: api-ms-win-crt-environment-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_hashlib.pyd 8350 WARNING: lib not found: api-ms-win-crt-convert-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_hashlib.pyd 8440 WARNING: lib not found: api-ms-win-crt-time-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_hashlib.pyd 8532 WARNING: lib not found: api-ms-win-crt-conio-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_hashlib.pyd 8622 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_hashlib.pyd 8733 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\select.pyd 8842 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_bz2.pyd 8933 WARNING: lib not found: api-ms-win-crt-math-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_bz2.pyd 9026 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_bz2.pyd 9123 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_bz2.pyd 9214 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_bz2.pyd 9330 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_lzma.pyd 9425 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_lzma.pyd 9538 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_socket.pyd 9631 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_socket.pyd 9636 INFO: Looking for eggs 9636 INFO: Using Python library c:\users\downtown\appdata\local\programs\python\python36\python36.dll 9636 INFO: Found binding redirects: [] 9640 INFO: Warnings written to D:\build\build\demo\warndemo.txt 9690 INFO: Graph cross-reference written to D:\build\build\demo\xref-demo.html 9708 INFO: checking PYZ 9709 INFO: Building PYZ because out00-PYZ.toc is non existent 9709 INFO: Building PYZ (ZlibArchive) D:\build\build\demo\out00-PYZ.pyz 10129 INFO: Building PYZ (ZlibArchive) D:\build\build\demo\out00-PYZ.pyz completed successfully. 10142 INFO: checking PKG 10142 INFO: Building PKG because out00-PKG.toc is non existent 10142 INFO: Building PKG (CArchive) out00-PKG.pkg 11623 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully. 11627 INFO: Bootloader c:\users\downtown\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe 11627 INFO: checking EXE 11629 INFO: Building EXE because out00-EXE.toc is non existent 11629 INFO: Building EXE from out00-EXE.toc 11639 INFO: Appending archive to EXE D:\build\dist\demo.exe 11653 INFO: Building EXE from out00-EXE.toc completed successfully. PS D:\build>
打开 dist 目录会看到已经生成了demo.exe
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步