把py转化成exe(干货)

article/2025/10/29 9:39:23

问题提出:

  无奈别人的电脑没有装python编译环境,无法运行py文件。
  本教程为在window环境下,使用PyInstaller3.4将Python脚本打包成exe可执行程序。

使用命令:

pip install pyinstaller
在这里插入图片描述
  上图为成功安装效果图,由红色框框看出:pyinstaller需要有altgraph-0.16.1 future-0.17.1 macholib-1.11 pefile-2019.4.18 pyinstaller-3.4 pywin32-ctypes-0.2.0支持。

用法:

usage: pyinstaller [-h] [-v] [-D] [-F] [--specpath DIR] [-n NAME][--add-data <SRC;DEST or SRC:DEST>][--add-binary <SRC;DEST or SRC:DEST>] [-p DIR][--hidden-import MODULENAME][--additional-hooks-dir HOOKSPATH][--runtime-hook RUNTIME_HOOKS] [--exclude-module EXCLUDES][--key KEY] [-d [{all,imports,bootloader,noarchive}]] [-s][--noupx] [-c] [-w][-i <FILE.ico or FILE.exe,ID or FILE.icns>][--version-file FILE] [-m <FILE or XML>] [-r RESOURCE][--uac-admin] [--uac-uiaccess] [--win-private-assemblies][--win-no-prefer-redirects][--osx-bundle-identifier BUNDLE_IDENTIFIER][--runtime-tmpdir PATH] [--bootloader-ignore-signals][--distpath DIR] [--workpath WORKPATH] [-y][--upx-dir UPX_DIR] [-a] [--clean] [--log-level LEVEL]scriptname [scriptname ...]

  下文底部有详解。

常用的参数:

• -F 表示生成单个可执行文件。
• -D –onedir 创建一个目录,包含exe文件,但会依赖很多文件(默认选项)。
• -w 表示去掉控制台窗口,这在GUI界面时非常有用。不过如果是命令行程序的话那就把这个。选项删除吧
• -c –console, –nowindowed 使用控制台,无界面(默认)。
• -p 表示你自己自定义需要加载的类路径,一般情况下用不到。
• -i 表示可执行文件的图标。

打包前:

在这里插入图片描述

  进入python需要打包的脚本所在目录,然后执行简单打包命令即可:

pyinstaller -F *.py

在这里插入图片描述
  会直接生成一个exe可执行文件。

运行结果:

在这里插入图片描述
  如果过程中出现某些问题,请自行google或者bing解决。

打包结果:

  打包完成后,进入到当前目录下,会发现多了build、dist、*.spec这四个文件夹或者文件,其中打包好的exe应用在dist目录下面,进入即可看到,可以把他拷贝到其他地方直接使用,如下图所示,是打包完成后的目录。
在这里插入图片描述

执行exe应用:

  因为是exe应用,是可执行文件了,所以直接双击运行即可。

进阶:

  还有其它你想要的功能可通过pyinstaller --help查看其他参数。

参数说明
optional arguments-hshow this help message and exit
-vshow this help message and exit
--distpath DIRWhere to put the bundled app (default: .\dist)
--workpath WORKPATHWhere to put all the temporary work files, .log, .pyzand etc. (default: .\build)
-y, --noconfirmReplace output directory (default: SPECPATH\dist\SPECNAME) without asking for confirmation
--upx-dir UPX_DIRPath to UPX utility (default: search the execution path)
-a, --asciiDo not include unicode encoding support (default: included if available)
--cleanClean PyInstaller cache and remove temporary files before building.
--log-level LEVELAmount of detail in build-time console messages. LEVEL,may be one of TRACE, DEBUG, INFO, WARN, ERROR, CRITICAL (default: INFO).
What to generate:-D, --onedirCreate a one-folder bundle containing an executable(default)
-F, --onefileCreate a one-file bundled executable.
--specpath DIRFolder to store the generated spec file (default:current directory)
-n NAME, --name NAMEName to assign to the bundled app and spec file(default: first script’s basename)
What to bundle, where to search:--add-data <SRC;DEST or SRC:DEST>Additional non-binary files or folders to be added to the executable. The path separator is platform specific, os.pathsep (which is ; on Windows and : on most unix systems) is used. This option can be used multiple times.
--add-binary <SRC;DEST or SRC:DEST>Additional binary files to be added to the executable. See the --add-data option for more details. This option can be used multiple times.
-p DIR, --paths DIRA path to search for imports (like using PYTHONPATH). Multiple paths are allowed, separated by ‘;’, or use this option multiple times
--hidden-import MODULENAME, --hiddenimport MODULENAMEName an import not visible in the code of the.script(s). This option can be used multiple times.
--additional-hooks-dir HOOKSPATHAn additional path to search for hooks. This option can be used multiple times.
--runtime-hook RUNTIME_HOOKSPath to a custom runtime hook file. A runtime hook is code that is bundled with the executable and is executed before any other code or module to set up special features of the runtime environment. This option can be used multiple times.
--exclude-module EXCLUDESOptional module or package (the Python name, not the path name) that will be ignored (as though it was not found). This option can be used multiple times.
--key KEYThe key used to encrypt Python bytecode.
How to generate:-d [{all,imports,bootloader,noarchive}], --debug [{all,imports,bootloader,noarchive}]Provide assistance with debugging a frozen application, by specifying one or more of the following choices.- all: All three of the below options; this is the default choice, unless one of the choices below is specified.- imports: specify the -v option to the underlying Python interpreter, causing it to print a message each time a module is initialized, showing the place (filename or built-in module) from which it is loaded. See https://docs.python.org/3/using/cmdline.html#id4. - bootloader: tell the bootloader to issue progress messages while initializing and starting the bundled app. Used to diagnose problems with missing imports.- noarchive: instead of storing all frozen Python source files as an archive inside the resulting executable, store them as files in the resulting output directory.
-s, --stripApply a symbol-table strip to the executable and shared libs (not recommended for Windows)
--noupxDo not use UPX even if it is available (works differently between Windows and *nix)
Windows and Mac OS X specific options:-c, --console, --nowindowedOpen a console window for standard i/o (default)
-w, --windowed, --noconsoleWindows and Mac OS X: do not provide a console window for standard i/o. On Mac OS X this also triggers building an OS X .app bundle. This option is ignored in *NIX systems.
-i <FILE.ico or FILE.exe,ID or FILE.icns>, --icon <FILE.ico or FILE.exe,ID or FILE.icns>FILE.ico: apply that icon to a Windows executable. FILE.exe,ID, extract the icon with ID from an exe. FILE.icns: apply the icon to the .app bundle on Mac OS X
Windows specific options:--version-file FILEadd a version resource from FILE to the exe
-m <FILE or XML>, --manifest <FILE or XML>add manifest FILE or XML to the exe
-r RESOURCE, --resource RESOURCEAdd or update a resource to a Windows executable. The RESOURCE is one to four items, FILE[,TYPE[,NAME[,LANGUAGE]]]. FILE can be a data file or an exe/dll. For data files, at least TYPE and NAME must be specified. LANGUAGE defaults to 0 or may be specified as wildcard * to update all resources of the given TYPE and NAME. For exe/dll files, all resources from FILE will be added/updated to the final executable if TYPE, NAME and LANGUAGE are omitted or specified as wildcard *.This option can be used multiple times.
--uac-adminUsing this option creates a Manifest which will request elevation upon application restart.
--uac-uiaccessUsing this option allows an elevated application to work with Remote Desktop.
Windows Side-by-side Assembly searching options (advanced):--win-private-assembliesAny Shared Assemblies bundled into the application will be changed into Private Assemblies. This means the exact versions of these assemblies will always be used, and any newer versions installed on user machines at the system level will be ignored.
--win-no-prefer-redirectsWhile searching for Shared or Private Assemblies to bundle into the application, PyInstaller will prefer not to follow policies that redirect to newer versions, and will try to bundle the exact versions of the assembly.
Mac OS X specific options:--osx-bundle-identifier BUNDLE_IDENTIFIERMac OS X .app bundle identifier is used as the default unique program name for code signing purposes. The usual form is a hierarchical name in reverse DNS notation. For example: com.mycompany.department.appname (default: first script’s basename)
Rarely used special options:--runtime-tmpdir PATHWhere to extract libraries and support files in ‘onefile’-mode. If this option is given, the bootloader will ignore any temp-folder location defined by the run-time OS. The `_MEIxxxxxx’-folder will be created here. Please use this option only if you know what you are doing.
--bootloader-ignore-signalsTell the bootloader to ignore signals rather than forwarding them to the child process. Useful in situations where e.g. a supervisor process signals both the bootloader and child (e.g. via a process group) to avoid signalling the child twice.

看我码这么多文字的份上点个赞再走啊~(❤ ω ❤)


http://chatgpt.dhexx.cn/article/VF4iElkk.shtml

相关文章

简单3步将你的python转成exe格式

使用pyinstaller可以将你的.py文件直接转换成.exe格式&#xff0c;在没有部署python环境的windows机器上直接运行你的程序&#xff01;&#xff01; 废话不多说&#xff0c;直接上正文。 1. 在命令行用pip安装 pyinstaller包 pip install pyinstaller 2.下载安装pyinstaler…

.py文件转为.exe文件的方法

写完一个Python程序想给朋友看&#xff1f;朋友电脑里没Python运行不了&#xff1f; 别急&#xff0c;这篇文章来帮你。 把你的python文件转成exe文件&#xff0c;打开方便&#xff0c;瞬间提示一个档次。 &#xff08;我个人也遇到过这样的问题&#xff0c;看了别人的博客解…

将Python程序打包成exe文件

我新写了一篇更加完整的文章&#xff0c;与这篇文章相比&#xff0c;它新增了两种打包方式&#xff1a;多python文件打包和含有资源文件的打包方式&#xff0c;具体请戳链接&#xff1a; 用 Pyinstaller 模块将 Python 程序打包成 exe 文件&#xff08;全网最全面最详细&#x…

Python 将.py转换为.exe详解

本文是由熊猫大哥大的博客修改而来&#xff08;所以就不能说是“原创”了&#xff09; 原博客链接&#xff1a;Python学习笔记(15)-Python代码转换为exe可执行程序详解&#xff08;下面会提到本文与其的一点不同&#xff09; 建议看看原博客&#xff0c;再看此文&#xff0c;…

Python学习笔记(15)-Python代码转换为exe可执行程序详解

点此查看 零基础Python全栈文章目录及源码下载 本文目录 1. 简介2. pyInstaller安装配置2.1 下载PyInstaller2.2 下载后解压PyInstaller2.3 下载pywin322.4 安装pywin322.5 安装PyInstaller 3. 制作exe3.1 编写python文件3.2 将代码文件放至pyinstaller目录3.3 制作exe3.4 查看…

如何将py文件转化为exe

如何将py文件转化为exe 第一步&#xff1a;安装pyinstaller库。 首先&#xff0c;winr打开运行窗口&#xff0c;输入 cmd&#xff0c;在cmd窗口中输入python -m pip install pyinstaller 等待计算机安装&#xff0c;完成后会有successfully等字眼 第二步&#xff1a;打包文件…

python文件转换成exe可执行的windows文件

一、介绍 python的程序需要运行环境有时候很不方便&#xff0c;因为要交给别人代码可能因为环境的原因运行各种问题&#xff0c;这里给出直接讲python程序转换成exe文件&#xff0c;很方便直接给执行程序就可以&#xff0c;也不用担心运行环境问题而运行不了 二、工具 1、安装…

将Python文件编译为exe可执行程序

Python程序py格式文件的优点是可以跨平台&#xff0c;但运行必须有Python环境&#xff0c;没有Python环境无法运行py格式文件。有没有方法&#xff0c;用户不同安装Python就可直接运行开发的项目工程&#xff1f;答案是肯定的。这就涉及到需要将Python的.py格式文件编写的脚本编…

python:将.py文件转换成.exe文件

一、使用pip安装PyInstaller ①python3.x需要使用PyInstaller才能进行转exe文件。 ②在命令里安装PyInstaller 如下图&#xff0c;我是在Anaconda的python环境下&#xff0c;直接在Anaconda的命令行窗口下&#xff0c;用命令pip install pyinstaller安装的。 也可以如下图&a…

如何将python程序打包成exe文件

将py打包为exe文件需要依赖pyinstaller 首先介绍以下pyinstaller的命令 -F&#xff1a;打包后只生成单个exe格式文件&#xff1b;-D&#xff1a;默认选项&#xff0c;创建一个目录&#xff0c;包含exe文件以及大量依赖文件&#xff1b;-c&#xff1a;默认选项&#xff0c;使用…

只需三步即可将 Python 程序转换成 exe 文件

只需三步即可将 Python 程序转换成 exe 文件 用pyinstaller可以将.py文件转换成.exe格式&#xff0c;让你的程序在没有安装python环境的windows上运行。一、在cmd命令行执行安装pyinstaller工具 pip install pyinstaller二、下载安装pyinstaler运行时所需windows扩展&#xf…

将python文件转成exe文件

通过 pyinstaller 将 python 文件 转成 exe 文件 第一步&#xff1a;推荐使用pip工具安装 pyinstaller cmd-- pip install pyinstaller 第二步&#xff1a; 在目标文件夹地址栏直接输入&#xff1a;cmd&#xff0c;此操作可以不用记住文件路径名称&#xff0c;直接在黑窗命令…

Python打包为exe文件

前言 我们开发的python脚本一般都会用到一些第三方包&#xff0c;当别人需要用到我们编写的脚本时&#xff0c;如果我们将脚本文件发给他&#xff0c;他是不能直接运行去用的&#xff0c;他需要安装python解释器&#xff0c;配置相应的环境&#xff0c;这是相当麻烦的。那怎么…

【Python】如何将写好的Python代码打包成exe文件?

&#x1f194;作者简介&#xff1a;一名电子信息大学生. &#x1f4d1; 个人主页&#xff1a;爱学习的汪同学主页&#x1f440; &#x1f4eb; 如果文章知识点有错误的地方,请指正&#xff01;和大家一起学习进步 &#x1f525; 如果感觉博主的文章还不错的话&#xff0c;还请不…

python文件转为可执行文件(.exe)的详细步骤

目录 六一快乐&#xff01;&#xff01;&#xff01;&#xff01; 前言 安装所需要的工具 关于“pyinstaller” 安装“pyinstaller” 准备python文件 打包python文件 运行可执行文件 注意事项&#xff01;&#xff01;&#xff01;&#xff01;&#xff01; 关于依赖项…

一次学会两种方式将 python 打成 exe

python 可以做网站应用&#xff0c;也可以做客户端应用。但是客户端应用需要运行 py 脚本&#xff0c;如果用户不懂 python 就是一件比较麻烦的事情。幸好 python 有第三方模块可以将脚本可以转成 exe 执行。 有些人可能要问了既然可以做成网站&#xff0c;为啥还要做成客户端…

手把手教你把Python代码转成exe

【文末有福利】 很多刚入门的盆友都有疑惑怎么把Python代码转成可执行的exe&#xff1f;&#xff0c;这里行哥统一解决一下这个问题 1.在cmd里安装pyintsaller包2.在cmd里输入打包成exe的命令3.问题总结4. pyintsaller --help5. end 1.在cmd里安装pyintsaller包 第一步安装将Py…

微服务架构服务治理:服务注册中心+服务发布与注册+服务发现与调用+服务监控

目录 一、服务注册中心&#xff1a;注册中心核心功能实现策略 1.注册中心核心功能 2.注册中心实现策略 二、服务发布与注册 三、服务发现与调用 四、服务监控 基本思路&#xff1a;日志埋点 基本目标&#xff1a; 基本定位&#xff1a; 基本策略&#xff1a; 具体实…

zabbix 监控 windows 服务器

zabbix 监控 windows 服务器 步骤&#xff1a; 1.确定zabbix服务器的IP地址和 zabbix 版本 2.官网下载对应版本的zabbix-agent或 3.在要监控的windows服务器中安装zabbix-agent 4.在zabbix配置里添加windows主机第一步&#xff1a;确定zabbix服务器的IP地址和 zabbix 版本 如…

java监听服务器信息,java实时监控服务器状态

java实时监控服务器状态 内容精选 换一换 监控是保持弹性云服务器可靠性、可用性和性能的重要部分,通过监控,用户可以观察弹性云服务器资源。为使用户更好地掌握自己的弹性云服务器运行状态,公有云平台提供了云监控。您可以使用该服务监控您的弹性云服务器,执行自动实时监控…