Python脚本导出为exe程序的方法


Posted in Python onMarch 25, 2020

一.pyinstaller简介

pyinstaller将Python脚本打包成可执行程序,使在没有Python环境的机器上运行

最新版是pyinstaller 3.1.1。支持python2.7和python3.3+。 可运行在Windows,Mac和Linux操作系统下。 但它不是跨编译的,也就是说在Windows下用PyInstaller生成的exe只能运行在Windows下,在Linux下生成的只能运行在Linux下。

二.pyinstaller在windows下的安装

使用命令pip install pyinstaller即可 在windows下,pyinstaller需要PyWin32的支持。当用pip安装pyinstaller时未找到PyWin32,会自动安装pypiwin32

Python脚本导出为exe程序的方法

Python脚本导出为exe程序的方法

出现Successfully installed pyinstaller-3.1.1 pypiwin32-219即表示安装成功

三.打包

打包的app里并不包含任何源码,但将脚本的.pyc文件打包了。

基本语法: pyinstaller options myscript.py

常用的可选参数如下:

?onefile 将结果打包成一个可执行文件
?onedir 将所有结果打包到一个文件夹中,该文件夹包括一个可执行文件和可执行文件执行时需要的依赖文件(默认)
?paths=DIR 设置导入路径
?distpath=DIR 设置将打包的结果文件放置的路径
?specpath=DIR 设置将spec文件放置的路径
?windowed 使用windows子系统执行,不会打开命令行(只对windows有效)
?nowindowed 使用控制台子系统执行(默认)(只对windows有效)
?icon=<FILE.ICO> 将file.ico添加为可执行文件的资源(只对windows有效)

如pyinstaller --paths=“D:\Queena” guess_exe.py

四.小实例(windows下)

写好游戏文件guess_exe.py,代码如下:

__author__ = 'zhou'
# -*- coding:utf-8 -*-
# 摇3次骰子,当总数total,3<=total<=10时为小,11<=total<=18为大
import random
import time
def enter_stake(current_money):
 '''输入小于结余的赌资及翻倍率,未考虑输入type错误的情况'''
 stake = int(input('How much you wanna bet?(such as 1000):'))
 rate = int(input("What multiplier do you want?你想翻几倍?(such as 2):"))
 small_compare = current_money < stake * rate
 while small_compare == True:
 stake = int(input('You has not so much money ${}!How much you wanna bet?(such as 1000):'.format(stake * rate)))
 rate = int(input("What multiplier do you want?你想翻几倍?(such as 2):"))
 small_compare = current_money < stake * rate
 return stake,rate
def roll_dice(times = 3):
 '''摇骰子'''
 print('<<<<<<<<<< Roll The Dice! >>>>>>>>>>')
 points_list = []
 while times > 0:
 number = random.randrange(1,7)
 points_list.append(number)
 times -= 1
 return points_list
def roll_result(total):
 '''判断是大是小'''
 is_big = 11 <= total <= 18
 is_small = 3 <= total <= 10
 if is_small:
 return 'Small'
 elif is_big:
 return 'Big'
def settlement(boo,points_list,current_money,stake = 1000,rate = 1):
 '''结余'''
 increase = stake * rate
 if boo:
 current_money += increase
 print('The points are ' + str(points_list) + ' .You win!')
 print('You gained $' + str(increase) + '.You have $' + str(current_money) + ' now.' )
 else:
 current_money -= increase
 print('The points are ' + str(points_list) + ' .You lose!')
 print('You lost $' + str(increase) + '.You have $' + str(current_money) + ' now.' )
 return current_money
def sleep_second(seconds=1):
 '''休眠'''
 time.sleep(seconds)
# 开始游戏
def start_game():
 '''开始猜大小的游戏'''
 current_money = 1000
 print('You have ${} now.'.format(current_money))
 sleep_second()
 while current_money > 0:
 print('<<<<<<<<<<<<<<<<<<<< Game Starts! >>>>>>>>>>>>>>>>>>>>')
 your_choice = input('Big or Small: ')
 choices = ['Big','Small']
 if your_choice in choices:
 stake,rate = enter_stake(current_money)
 points_list = roll_dice()
 total = sum(points_list)
 actual_result = roll_result(total)
 boo = your_choice == actual_result
 current_money = settlement(boo,points_list,current_money,stake,rate)
 else:
 print('Invalid input!')
 else:
 sleep_second()
 print('Game Over!')
 sleep_second(2)
if __name__ == '__main__':
 start_game()

之后命令行,切换到guess_exe.py的目录下, 输入:

pyinstaller --onefile --nowindowed --icon="D:\Queena\PyCharmProjects\dist1\computer_three.ico" guess_exe.py

Python脚本导出为exe程序的方法
Python脚本导出为exe程序的方法

就会在当前文件下形成build文件夹、dist文件夹和.spec文件。 dist里就是guess_exe.exe可执行文件。

[外链图片转存失败(img-NSV511rc-1562767762570)(https://upload-images.jianshu.io/upload_images/6152595-56dc5aad9152513e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]

1、安装pyinstaller(需要先安装pip)、再:pip install pyinstaller

(由于我事先安装了pyinstaller,为了方便就卸载了,不知道影不影响显示。但安装成功后会有“Successfully installed pyinstaller”的提示)

Python脚本导出为exe程序的方法

2、定位到pyinstaller.exe所在文件夹(一般再python下的“scripts”文件夹下)

(温馨提示:再cmd下tab键又补全功能哦)

Python脚本导出为exe程序的方法

3、再添加上你要转换的文件地址(两者之间有空格)

pyinstaller.exe后面如果加上-F就是打包为一个exe文件(文件会比较大),如果不加就会有很多库文件;加上-w就是打包为没有cmd窗口的exe,不加运行时就会出现cmd窗口。(加不加凭个人喜好)

Python脚本导出为exe程序的方法

4. 加-F的效果

Python脚本导出为exe程序的方法

不加-F

Python脚本导出为exe程序的方法

不加-w的效果

(加-w的话,就没有后面的那个黑框了

Python脚本导出为exe程序的方法

1、-F指令

注意指令区分大小写。这里是大写。使用-F指令可以把应用打包成一个独立的exe文件,否则是一个带各种dll和依赖文件的文件夹

Python脚本导出为exe程序的方法

2、-p指令

这个指令后面可以增加pyinstaller搜索模块的路径。因为应用打包涉及的模块很多。这里可以自己添加路径。不过经过笔者测试,site-packages目录下都是可以被识别的,不需要再手动添加

Python脚本导出为exe程序的方法

补充:如何将python的.py文件转换为可执行的.exe文件。

首先,我写了一个print(“hello,world”).py文件。命名为hello.py保存在我的电脑C盘的C:\Users\ly目录下如图所示。

ps:尽量选择在这个文件夹下,如果选择其他盘的文件夹下,生成的.exe的dist文件夹也会出现在这个c盘的路径下,而且如果保存在其他盘下有时候还会出错,不好用。

Python脚本导出为exe程序的方法              Python脚本导出为exe程序的方法

 

利用pip安装python的工具库pyinstaller。

pip install pyinstaller

安装成功后

在命令窗口输入:pyinstaller -F C:\Users\ly\hello.py

注意 F 一定要大写

然后就会在这个路径下的dist文件夹下找到这和同名的hello.exe文件。

Python脚本导出为exe程序的方法

总结

到此这篇关于Python脚本导出为exe程序的方法的文章就介绍到这了,更多相关Python导出exe程序内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python 调用VC++的动态链接库(DLL)
Sep 06 Python
Python logging模块学习笔记
May 24 Python
python里对list中的整数求平均并排序
Sep 12 Python
python中利用zfill方法自动给数字前面补0
Apr 10 Python
python调用摄像头显示图像的实例
Aug 03 Python
Python列表list排列组合操作示例
Dec 18 Python
使用Python实现将list中的每一项的首字母大写
Jun 11 Python
Python QQBot库的QQ聊天机器人
Jun 19 Python
python递归法实现简易连连看小游戏
Mar 25 Python
增大python字体的方法步骤
Jul 05 Python
Python环境搭建过程从安装到Hello World
Feb 05 Python
利用Python读取微信朋友圈的多种方法总结
Aug 23 Python
Python实现猜年龄游戏代码实例
Mar 25 #Python
python读取mysql数据绘制条形图
Mar 25 #Python
Python环境下安装PyGame和PyOpenGL的方法
Mar 25 #Python
python画环形图的方法
Mar 25 #Python
Python面向对象魔法方法和单例模块代码实例
Mar 25 #Python
Python语法垃圾回收机制原理解析
Mar 25 #Python
python实现Pyecharts实现动态地图(Map、Geo)
Mar 25 #Python
You might like
PHP随机生成随机个数的字母组合示例
2014/01/14 PHP
CodeIgniter中使用cookie的三种方式详解
2014/07/18 PHP
smarty自定义函数htmlcheckboxes用法实例
2015/01/22 PHP
php两点地理坐标距离的计算方法
2018/12/29 PHP
Thinkphp 3.2框架使用Redis的方法详解
2019/10/24 PHP
详解phpstorm2020最新破解方法
2020/09/17 PHP
javascript Object与Function使用
2010/01/11 Javascript
js动态拼接正则表达式的两种方法
2014/03/04 Javascript
javascript编写贪吃蛇游戏
2015/07/07 Javascript
JS实现完全语义化的网页选项卡效果代码
2015/09/15 Javascript
向JavaScript的数组中添加元素的方法小结
2015/10/24 Javascript
深入理解JavaScript程序中内存泄漏
2016/03/17 Javascript
AngularJS控制器之间的通信方式详解
2016/11/03 Javascript
JS实现数组去重复值的方法示例
2017/02/18 Javascript
ES6深入理解之“let”能替代”var“吗?
2017/06/28 Javascript
vuejs手把手教你写一个完整的购物车实例代码
2017/07/06 Javascript
nodejs提示:cross-device link not permitted, rename错误的解决方法
2019/06/10 NodeJs
JS使用正则表达式提交页面验证的代码
2019/10/16 Javascript
JS删除对象中某一属性案例详解
2020/09/08 Javascript
python实现简单socket通信的方法
2016/04/19 Python
Python错误提示:[Errno 24] Too many open files的分析与解决
2017/02/16 Python
浅谈flask源码之请求过程
2018/07/26 Python
django中media媒体路径设置的步骤
2019/11/15 Python
TensorFlow实现指数衰减学习率的方法
2020/02/05 Python
Html5 webview元素定位工具的实现
2020/08/07 HTML / CSS
学校后勤人员职责
2013/12/27 职场文书
八一建军节活动方案
2014/02/10 职场文书
亲属关系公证书
2014/04/08 职场文书
竞选部长演讲稿
2014/04/26 职场文书
单位活动策划方案
2014/08/17 职场文书
祖国在我心中演讲稿200字
2014/08/28 职场文书
优秀班组申报材料
2014/12/25 职场文书
兴趣班停课通知
2015/04/24 职场文书
2016年暑期见闻作文
2015/11/25 职场文书
小学班主任培训心得体会
2016/01/07 职场文书
AJAX实现省市县三级联动效果
2021/10/16 Javascript