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


    一、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()
    

    五、pyinstaller打包

    E:>pyinstaller -F test.py       # 有input函数的时候,就不带-w,不然会报错。
    E:>pyinstaller -F -w test.py   

    https://www.wukong.com/answer/6732408567304814861/

    https://www.cnblogs.com/robinunix/p/8426832.html

  • 相关阅读:
    logstash收集nginx日志写入kafka
    Logstash收集日志写入Redis
    Nginx反向代理kibana实现认证访问
    logstash收集nginx日志写入kafka1
    201999:渗透测试,基础学习,windows基础命令,笔记
    2019910:渗透测试,基础学习
    中华吸血鬼恶意病毒分析
    磁碟机病毒分析
    利用SQL语句对不同数据库进行高效果分页
    TimeBased Blind SQL Injection with Heavy Queries
  • 原文地址:https://www.cnblogs.com/liuhaidon/p/12074370.html
Copyright © 2020-2023  润新知