如何把Python脚本导出为exe程序

一.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

image.png

image.png

出现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=

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()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
之后命令行,切换到guess_exe.py的目录下, 输入:

pyinstaller --onefile --nowindowed --icon=“D:\Queena\PyCharmProjects\dist1\computer_three.ico” guess_exe.py
1
image.png
image.png

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

image.png

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

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

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

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

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

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

image.png
4. 加-F的效果

image.png

不加-F
image.png

不加-w的效果

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

image.png

1、-F指令

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

image.png

2、-p指令

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

image.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值