欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python—脚本程序生成exe可执行程序(pyinstaller)

程序员文章站 2022-05-29 08:26:06
一、pyinstaller的简介 Python是一个脚本语言,被解释器解释执行。它的发布方式: .py文件:对于开源项目或者源码没那么重要的,直接提供源码,需要使用者自行安装Python并且安装依赖的各种库。(Python官方的各种安装包就是这样做的)。 .pyc文件:有些公司或个人因为机密或者各种 ......

一、pyinstaller的简介

python是一个脚本语言,被解释器解释执行。它的发布方式:

  • .py文件:对于开源项目或者源码没那么重要的,直接提供源码,需要使用者自行安装python并且安装依赖的各种库。(python官方的各种安装包就是这样做的)。
  • .pyc文件:有些公司或个人因为机密或者各种原因,不愿意源码被运行者看到,可以使用pyc文件发布,pyc文件是python解释器可以识别的二进制码,故发布后也是跨平台的,需要使用者安装相应版本的python和依赖库。
  • 可执行文件:对于一些小白用户,最简单的方式就是提供一个可执行文件,只需要把用法告诉ta即可。比较麻烦的是需要针对不同平台需要打包不同的可执行文件(windows,linux,mac,...)。

二、pyinstaller的原理简介

三、pyinstaller的安装

[root@localhost ~]# pip install pyinstaller

四、小实例(windows下)

# -*- coding:utf-8 -*-
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))
    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()