windows下使用pyinstaller把python文件打包成exe可执行文件

时间:2023-11-20 10:56:44

使用pyinstaller打包有个好处就是所有依赖都打包进去了,可以随意把文件移动到别的电脑上使用

安装

pip install pyinstaller

新建一个demo.py文件

#!/usr/bin/python3
# -*- coding: utf-8 -*-
print("hello world")

打开cmd或powershell

windows下使用pyinstaller把python文件打包成exe可执行文件

PS D:\build> pyinstaller.exe -F -c .\demo.py

 INFO: PyInstaller: 3.3.
INFO: Python: 3.6.
INFO: Platform: Windows--10.0.-SP0
INFO: wrote D:\build\demo.spec
INFO: UPX is not available.
INFO: Extending PYTHONPATH with paths
['D:\\build', 'D:\\build']
INFO: checking Analysis
INFO: Building Analysis because out00-Analysis.toc is non existent
INFO: Initializing module dependency graph...
INFO: Initializing module graph hooks...
INFO: Analyzing base_library.zip ...
INFO: running Analysis out00-Analysis.toc
INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by c:\users\downtown\appdata\local\programs\python\python36\python.exe
WARNING: lib not found: api-ms-win-crt-heap-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python.exe
WARNING: lib not found: api-ms-win-crt-runtime-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python.exe
WARNING: lib not found: api-ms-win-crt-stdio-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python.exe
WARNING: lib not found: api-ms-win-crt-locale-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python.exe
WARNING: lib not found: api-ms-win-crt-math-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python.exe
WARNING: lib not found: api-ms-win-crt-heap-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll
WARNING: lib not found: api-ms-win-crt-runtime-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll
WARNING: lib not found: api-ms-win-crt-environment-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll
WARNING: lib not found: api-ms-win-crt-convert-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll
WARNING: lib not found: api-ms-win-crt-locale-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll
WARNING: lib not found: api-ms-win-crt-time-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll
WARNING: lib not found: api-ms-win-crt-stdio-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll
WARNING: lib not found: api-ms-win-crt-process-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll
WARNING: lib not found: api-ms-win-crt-conio-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll
WARNING: lib not found: api-ms-win-crt-string-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll
WARNING: lib not found: api-ms-win-crt-math-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll
WARNING: lib not found: api-ms-win-crt-filesystem-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\python36.dll
WARNING: lib not found: api-ms-win-crt-runtime-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\VCRUNTIME140.dll
WARNING: lib not found: api-ms-win-crt-stdio-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\VCRUNTIME140.dll
WARNING: lib not found: api-ms-win-crt-convert-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\VCRUNTIME140.dll
WARNING: lib not found: api-ms-win-crt-string-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\VCRUNTIME140.dll
WARNING: lib not found: api-ms-win-crt-heap-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\VCRUNTIME140.dll
INFO: Caching module hooks...
INFO: Analyzing D:\build\demo.py
INFO: Loading module hooks...
INFO: Loading module hook "hook-encodings.py"...
INFO: Loading module hook "hook-pydoc.py"...
INFO: Loading module hook "hook-xml.py"...
INFO: Looking for ctypes DLLs
INFO: Analyzing run-time hooks ...
INFO: Looking for dynamic libraries
WARNING: lib not found: api-ms-win-crt-heap-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_ssl.pyd
WARNING: lib not found: api-ms-win-crt-runtime-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_ssl.pyd
WARNING: lib not found: api-ms-win-crt-utility-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_ssl.pyd
WARNING: lib not found: api-ms-win-crt-stdio-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_ssl.pyd
WARNING: lib not found: api-ms-win-crt-environment-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_ssl.pyd
WARNING: lib not found: api-ms-win-crt-convert-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_ssl.pyd
WARNING: lib not found: api-ms-win-crt-time-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_ssl.pyd
WARNING: lib not found: api-ms-win-crt-filesystem-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_ssl.pyd
WARNING: lib not found: api-ms-win-crt-conio-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_ssl.pyd
WARNING: lib not found: api-ms-win-crt-string-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_ssl.pyd
WARNING: lib not found: api-ms-win-crt-runtime-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\unicodedata.pyd
WARNING: lib not found: api-ms-win-crt-stdio-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\unicodedata.pyd
WARNING: lib not found: api-ms-win-crt-string-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\unicodedata.pyd
WARNING: lib not found: api-ms-win-crt-heap-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\pyexpat.pyd
WARNING: lib not found: api-ms-win-crt-runtime-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\pyexpat.pyd
WARNING: lib not found: api-ms-win-crt-environment-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\pyexpat.pyd
WARNING: lib not found: api-ms-win-crt-stdio-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\pyexpat.pyd
WARNING: lib not found: api-ms-win-crt-string-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\pyexpat.pyd
WARNING: lib not found: api-ms-win-crt-heap-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_hashlib.pyd
WARNING: lib not found: api-ms-win-crt-runtime-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_hashlib.pyd
WARNING: lib not found: api-ms-win-crt-utility-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_hashlib.pyd
WARNING: lib not found: api-ms-win-crt-stdio-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_hashlib.pyd
WARNING: lib not found: api-ms-win-crt-environment-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_hashlib.pyd
WARNING: lib not found: api-ms-win-crt-convert-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_hashlib.pyd
WARNING: lib not found: api-ms-win-crt-time-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_hashlib.pyd
WARNING: lib not found: api-ms-win-crt-conio-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_hashlib.pyd
WARNING: lib not found: api-ms-win-crt-string-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_hashlib.pyd
WARNING: lib not found: api-ms-win-crt-runtime-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\select.pyd
WARNING: lib not found: api-ms-win-crt-runtime-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_bz2.pyd
WARNING: lib not found: api-ms-win-crt-math-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_bz2.pyd
WARNING: lib not found: api-ms-win-crt-stdio-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_bz2.pyd
WARNING: lib not found: api-ms-win-crt-string-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_bz2.pyd
WARNING: lib not found: api-ms-win-crt-heap-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_bz2.pyd
WARNING: lib not found: api-ms-win-crt-runtime-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_lzma.pyd
WARNING: lib not found: api-ms-win-crt-heap-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_lzma.pyd
WARNING: lib not found: api-ms-win-crt-runtime-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_socket.pyd
WARNING: lib not found: api-ms-win-crt-string-l1--.dll dependency of c:\users\downtown\appdata\local\programs\python\python36\DLLs\_socket.pyd
INFO: Looking for eggs
INFO: Using Python library c:\users\downtown\appdata\local\programs\python\python36\python36.dll
INFO: Found binding redirects:
[]
INFO: Warnings written to D:\build\build\demo\warndemo.txt
INFO: Graph cross-reference written to D:\build\build\demo\xref-demo.html
INFO: checking PYZ
INFO: Building PYZ because out00-PYZ.toc is non existent
INFO: Building PYZ (ZlibArchive) D:\build\build\demo\out00-PYZ.pyz
INFO: Building PYZ (ZlibArchive) D:\build\build\demo\out00-PYZ.pyz completed successfully.
INFO: checking PKG
INFO: Building PKG because out00-PKG.toc is non existent
INFO: Building PKG (CArchive) out00-PKG.pkg
INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
INFO: Bootloader c:\users\downtown\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
INFO: checking EXE
INFO: Building EXE because out00-EXE.toc is non existent
INFO: Building EXE from out00-EXE.toc
INFO: Appending archive to EXE D:\build\dist\demo.exe
INFO: Building EXE from out00-EXE.toc completed successfully.
PS D:\build>

打开 dist 目录会看到已经生成了demo.exe