How to export a Python script as an exe program

1. Introduction to pyinstaller

pyinstaller packages Python scripts into executable programs to run on machines without a Python environment

The latest version is pyinstaller 3.1.1. Support python2.7 and python3.3+. Runs on Windows, Mac and Linux operating systems. But it is not cross-compiled, that is to say, the exe generated by PyInstaller under Windows can only run under Windows, and the exe generated under Linux can only run under Linux.

2. Installation of pyinstaller under windows

Use the command  pip install pyinstaller  to run under windows, pyinstaller needs the support of PyWin32. When PyWin32 is not found when pyinstaller is installed with pip, pypiwin32 is automatically installed

 
 
 
 

Successfully installed pyinstaller-3.1.1 pypiwin32-219 means the installation is successful

3. Packing

The packaged app does not contain any source code, but the .pyc file of the script is packaged.

Basic syntax:   pyinstaller options myscript.py 

Commonly used optional parameters are as follows:
--onefile package the results into an executable file
--onedir package all the results into a folder that includes an executable file and the dependencies required for the executable to execute ( Default)
--paths=DIR Set the import path
--distpath=DIR Set the path where the packaged result file will be placed
--specpath=DIR Set the path where the spec file will be placed --windowed
Use the windows subsystem to execute without opening the command line (only valid for windows)
--nowindowed use the console subsystem to execute (default) (only valid for windows)
--icon=<FILE.ICO> Add file.ico as a resource of the executable file (only valid for windows)

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

4. Small example (under Windows)

Write the game file guess_exe.py, the code is as follows:

__author__ = 'zhou'

# -*- coding:utf-8 -*-

#Shake the dice 3 times, when the total number is total, 3<=total<=10 is small, 11<=total<=18 is big

import random

import time

def enter_stake(current_money):

 ''' Enter the gambling capital and doubling rate less than the balance, ignoring the case of wrong input type '''

 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):

 ''' Shake the 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):

 ''' Judgment is 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):

 ''' Hibernate '''

 time.sleep(seconds)

#start game

def start_game():

 ''' Start the guessing 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()

 

After the command line, switch to the directory of guess_exe.py and enter:

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

 

 
 

 

 
 

The build folder, dist folder and .spec file will be formed under the current file. Dist is the guess_exe.exe executable file.

 
 

1. Install pyinstaller (need to install pip first), then:  pip install pyinstaller 

(Because I installed pyinstaller in advance, I uninstalled it for convenience. I don't know if it will affect the display. But after the installation is successful, there will be a prompt of "Successfully installed pyinstaller")

 
 

 

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

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

 
 

 

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

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

 
 
  1. 加-F的效果
 
 
  1. 不加-F


     
     
  2. 不加-w的效果

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

 
 

1、-F指令

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

 
 

2、-p指令

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

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325106494&siteId=291194637