• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

使用Pyinstaller转换.py文件为.exe可执行程序过程详解

python 搞代码 4年前 (2022-01-09) 15次浏览 已收录 0个评论

 前言

pyinstaller能够在Windows、Linux等操作系统下将Python脚本打包成可直接运行程序。使Python脚本可以在没有安装Python的环境中直接运行,方便共享。

开发环境

python 2.7.12 + Windows7

注意事项

1、待转换的.py文件绝对路径最好不要包含中文字符。容易出现一些莫名其妙的问题。

2、python中需要有.py文件中用到的第三方库。否则在转换后的.exe文件中会出现不符合预期的结果。

pyinstaller安装步骤

1、配置pip镜像源。pip配置方法参考pip配置和安装第三方模块。如果已经配置,跳过。

2、打开cmd命令行窗口,输入pip install pyinstaller,安装pyinstaller库。

C:\Users\Administrator>pip install pyinstaller
Collecting pyinstaller
 Downloading http://pypi.doubanio.com/packages/3c/86/909a8c35c5471919b3854c01f43843d9b5aed0e9948b63e560010f7f3429/PyIns
taller-3.3.1.tar.gz (3.5MB)
  100% |????????????????????????????????| 3.5MB 112kB/s
Requirement already satisfied: setuptools in c:\python27\lib\site-packages (from pyinstaller)
Requirement already satisfied: pefile>=2017.8.1 in c:\python27\lib\site-packages (from pyinstaller)
Requirement already satisfied: macholib>=1.8 in c:\python27\lib\site-packages (from pyinstaller)
Requirement already satisfied: dis3 in c:\python27\lib\site-packages (from pyinstaller)
Requirement already satisfied: future in c:\python27\lib\site-packages (from pyinstaller)
Requirement already satisfied: altgraph>=0.15 in c:\python27\lib\site-packages (from macholib>=1.8->pyinstaller)
Installing collected packages: pyinstaller
 Running setup.py install for pyinstaller ... done
Successfully installed pyinstaller-3.3.1

3、确认pyinstaller安装结果,位于c:\Python27\Scripts路径下。执行where pyinstaller查看

C:\Users>where pyinstaller
c:\Python27\Scripts\pyinstaller.exe

pyinstaller基本语法

pyinstaller [options] script

options常用选项说明:

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

基本实例:pyinstaller -F myscript.py。

pyinstaller更多语法见官网说明: https://pyinstaller.readthed本文来源[email protected]搞@^&代*@码网(ocs.io/en/stable/usage.html

pyinstaller原理简介

pyinstaller其实就是把python解释器和脚本打包成一个可执行文件,和编译成真正的机器码是完全两回事。所以打包不一定会提高运行效率,可能会降低运行效率,但是好处是在运行者机器上不用安装python和脚本所依赖的库。

输入指定的脚本后,首先pyinstaller会分析该脚本所依赖的其他依赖,然后进行查找、复制,把所有相关的依赖都收集起来并惊醒加密处理,包括python解释器,最后把这些文件放在一个目录下,或者打包到一个可执行文件。然后就可以直接运行所生成的可执行文件。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:使用Pyinstaller转换.py文件为.exe可执行程序过程详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址