How to export a Python script as an EXE program

Source: Internet
Author: User
Tags python script

Reprinted from: http://www.jianshu.com/p/8dbdfbd3716d

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

Use the command pip install Pyinstaller to
Under Windows, Pyinstaller needs 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__='qa-2'#-*-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.

If there is a packing error, look at the warn in the build *A. txt document that details the cause of the error. Generally, the library is missing.
The spec file tells Pyinstaller how to handle the script. It encrypts the script name and the optional parameters of most pyinstaller. Pyinstaller is to build the app by executing the content of the spec file. Five. Experience in Muggle programming:

My greatest impression is the beauty of the Magic tutorial, which is clear, concise and easy to understand.
Video learning process in the deep experience of the creator's intentions, exquisite matching picture with appropriate background music, basic knowledge and appropriate small projects, this series of supporting achievements of the Muggle's extraordinary.
After the successful learning of the 100,000 data, the sense of accomplishment is simply beyond words. After the visualization of the data and a variety of graphics and display on the Web page, this series of achievements have made me very happy, and this skill let me in the professional technology has a great upgrade, after the job-hopping I have the capital, thanks to Muggles!

Six. Reference website:

http://pythonhosted.org/PyInstaller/
http://blog.csdn.net/zc02051126/article/details/8827868

How to export a Python script as an EXE program

Related Article

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.