How to export a Python script as an EXE program

Source: Internet
Author: User
Tags python script

I. Pyinstaller INTRODUCTION

Pyinstaller a Python script into an executable program that runs on a machine without a python environment

The latest version is Pyinstaller 3.1.1. Support python2.7 and python3.3+. Can run under WINDOWS,MAC and Linux operating systems. But it is not cross-compiled, that is, under Windows with Pyinstaller generated EXE can only run under Windows, Linux under the build can only run under Linux.

Two. Pyinstaller installation under Windows

Using the command pip install Pyinstaller is available under Windows, Pyinstaller requires PYWIN32 support. When you install Pyinstaller with PIP, PyWin32 is not found and is automatically installed Pypiwin32

  

Successfully installed pyinstaller-3.1.1 pypiwin32-219 indicates successful installation

Three. Packaging

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

Basic syntax: pyinstaller options myscript.py

The following optional parameters are commonly used:
--onefile Package The results into an executable file
--onedir Package All results into a folder that includes an executable file and the dependent files required to execute the executable file (default)
--paths=dir Setting the import path
--distpath=dir Set the path to place the packaged results file
--specpath=dir Setting the path to place the spec file
--windowed is executed using Windows subsystem and does not open the command line (only valid for Windows)
--nowindowed using the console subsystem (default) (only valid for Windows)
--icon=<file. ico> add File.ico as an executable resource (only valid for Windows)

such as Pyinstaller--paths="D:\Queena" guess_exe.py

Four. Small instance (under Windows)

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

__author__='Zhou'#-*-coding:utf-8-*-#Roll 3 Dice, when the total total,3<=total<=10 is small, 11<=total<=18 for largeImportRandomImport TimedefEnter_stake (Current_money):" "input gambling and magnification less than the balance, regardless of input type error" "stake= Int (Input ('How much wanna bet? (such as):')) rate= Int (Input ("What multiplier does you want? How few times you want to flip? (such as 2):")) Small_compare= Current_money < Stake * Rate whileSmall_compare = =True:stake= Int (Input ('You have much money ${}! How much wanna bet? (such as):'. Format (Stake *Rate)) ) Rate= Int (Input ("What multiplier does you want? How few times you want to flip? (such as 2):")) Small_compare= Current_money < Stake * Ratereturnstake,ratedefRoll_dice (Times = 3): " "Roll the Dice" " Print('<<<<<<<<<< Roll the dice! >>>>>>>>>>') Points_list= []  whileTimes >0:number= Random.randrange (1,7) points_list.append (number) times-= 1returnpoints_listdefRoll_result (total):" "judgment is big or small" "Is_big= <= Total <= 18Is_small= 3 <= Total <= 10ifIs_small:return 'Small' elifIs_big:return 'Big'defSettlement (Boo,points_list,current_money,stake = 1000,rate = 1): " "Balance" "Increase= Stake * RateifBoo:current_money+=IncreasePrint('The points is'+ STR (points_list) +' . You win!') Print('You gained $'+ str (Increase) +'. you have $'+ STR (Current_money) +'Now .' ) Else: Current_money-=IncreasePrint('The points is'+ STR (points_list) +' . You lose!') Print('You lost $'+ str (Increase) +'. you have $'+ STR (Current_money) +'Now .' ) returnCurrent_moneydefSleep_second (Seconds=1): " "Hibernate" "time.sleep (seconds)#Start the gamedefstart_game ():" "start guessing the size of the game" "Current_money= 1000Print('You are ${} now.'. Format (Current_money)) Sleep_second () whileCurrent_money >0:Print('<<<<<<<<<<<<<<<<<<<< Game starts! >>> >>>>>>>>>>>>>>>>>') Your_choice= Input ('Big or Small:') Choices= ['Big','Small'] ifYour_choiceinchchoices: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 guess_exe.py directory and enter:

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

 

 

The build folder, the Dist folder, and the. spec file are 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 the Pyinstaller in advance, in order to facilitate the uninstall, do not know the shadow does not affect the display.) However, after successful installation there will be "successfully installed Pyinstaller" prompt)

 

2. Locate the folder where Pyinstaller.exe is located (under "Scripts" folder under Python)

(Warm hint: Again cmd next TAB key and complete function OH)

 

3. Add the file address you want to convert (there are spaces between them)

Pyinstaller.exe back if add-f is packaged as an EXE file (the file will be larger), if not added there will be many library files, plus-W is packaged as an EXE without a cmd window, the cmd window will appear without running. (plus no personal preference)

 
    1. Add-F Effect
 
    1. Do not add-f


    2. No-W effect

(Plus-W, there's no black box in the back.)

 

1.-F Instruction

Note the directives are case-sensitive. This is uppercase. Use the-F directive to package your app as a standalone EXE file, or a folder with various DLLs and dependent files

 

2.-P Instruction

You can add the path to the Pyinstaller search module later in this instruction. Because there are many modules involved in app packaging. Here you can add the path yourself. But after the author tests, the Site-packages directory can be recognized, do not need to manually add

 

How to export a Python script as an EXE program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.