Python - script program generates exe executable program (pyinstaller)

Keywords: Python Windows Linux Mac

1, About pyinstaller

Python is a scripting language that is interpreted and executed by an interpreter. How to publish it:

  • . py file: for open source projects or sources that are not so important, providing source directly requires users to install Python and install various libraries they depend on. (this is what Python's official packages do.).
  • . pyc file: some companies or individuals do not want the source code to be seen by the operator because of confidentiality or various reasons. They can use pyc file to publish. pyc file is binary code recognized by Python interpreter, so it is also cross platform after publishing. Users need to install the corresponding version of Python and dependency library.
  • Executable file: for some small white users, the easiest way is to provide an executable file, just tell Ta the usage. It is troublesome to pack different executable files (Windows,Linux,Mac,...) for different platforms.

2, A brief introduction to PyInstaller

3, pyinstaller installation

[root@localhost ~]# pip install pyinstaller

4, Small instance (under windows)

# -*- coding:utf-8 -*-
import random
import time

def enter_stake(current_money):
    '''Input bet capital and doubling rate less than balance,Input not considered type Wrong situation'''
    stake = int(input('How much you wanna bet?(such as 1000):'))
    rate = int(input("What multiplier do you want?How many times 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?How many times do you want?(such as 2):"))
        small_compare = current_money < stake * rate
    return stake,rate

def roll_dice(times = 3):
    '''Dice'''
    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):
    '''Judge big or small'''
    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):
    '''Balance'''
    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):
    '''dormancy'''
    time.sleep(seconds)

def start_game():
    '''Start to guess the size of the 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()

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

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

Posted by goatboy on Fri, 20 Dec 2019 07:35:55 -0800