How to export a Python script as an EXE program

Source: Internet
Author: User
Tags python script

I. Pyinstaller INTRODUCTION

Python is a scripting language that is interpreted by the interpreter to execute. How it is published:

    • . py file: For open source projects or source code is not so important, directly provide the source code, users need to install Python itself and install a variety of dependent libraries. (This is the case with all the official Python installation packages)

    • . pyc file: Some companies or individuals because of confidential or various reasons, not willing to source code is seen by the runtime, you can use the PYc file publishing, PYc file is the Python interpreter can recognize the binary code, so after publishing is cross-platform, users need to install the corresponding version of Python and dependent library.

    • Executable file: For non-code farmers or some small white users, you let him to install a python and also toss a bunch of dependent libraries, it is a disaster. For this type of user, the simplest way is to provide an executable file, just tell TA to use it. The trouble is that you need to package different executables for different platforms (Windows,linux,mac,... )。

This article is mainly about the last way,. Py and. PYc are relatively simple, python itself can be done. There are several ways to package Python scripts into executables, and this article focuses on Pyinstaller,

Introduction to the principle of pyinstaller

Pyinstaller is actually the Python parser and your own scripts packaged into an executable file, and compiled into a real machine code is completely different, so do not expect to be packaged into an executable file will improve the efficiency of operation, the contrary may reduce operational efficiency, The advantage is that you do not have to install Python and the libraries that your scripts rely on on the runner's machine. Under the Linux operating system, it mainly uses the binutil inside of the toolkit ldd and objdump commands.

Pyinstaller Enter the script you specify, first analyze the other scripts that the script depends on, and then go to find, copy, and collect all the relevant scripts, including the Python parser, and then put the files in a directory, or package them in an executable file.

You can publish directly the files in the entire folder of the output, or the resulting executable file. You just have to tell the user that your app is self-contained, that you don't need to install another package, or a version of Python, and you can run it directly.

It is important to note that Pyinstaller packaged execution files can only be used in the same environment as the packaging machine system. That is, it is not portable and must be packaged for the platform if it needs to be run on a different system.

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 total total,3<=total<=10 is small, 11<=total<=18 for bigImport RandomImport timeDefEnter_stake(Current_money):"' Enter gambling and magnification less than the balance and do not consider 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 turn?" (such as 2): ") Small_compare = Current_money < stake * RateWhile small_compare = =true:stake = Int (input (' You have not so 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 turn?" (such as 2): ") Small_compare = Current_money < stake * RateReturn stake,rateDefRoll_dice(Times =3):"Roll The Dice" print (' <<<<<<<<<< Roll the dice! >>>>>>>>>> ') points_list = []While times >0:number = Random.randrange (1,7) points_list.append (number) times-=1Return points_listDefRoll_result(total):' Judgment is big or small ' Is_big =<= Total <=Is_small =3 <= Total <=10If Is_small:Return' Small 'Elif Is_big:Return' Big 'DefSettlement(Boo,points_list,current_money,stake =1000,rate =1):' balance ' increase = stake * RateIf Boo:current_money + = increase print (' The points is ' + str (points_list) +‘ . You win! ') Print' You gained $ ' + str (Increase) +‘. You have $ ' + str (Current_money) +' Now. ')Else:current_money-= increase print (' The points is ' + str (points_list) +‘ . You lose! ') Print' You lost $ ' + str (Increase) +‘. You have $ ' + str (Current_money) +' Now. ')Return Current_moneyDefSleep_second(seconds=1):"Sleep" time.sleep (seconds)# Start the gameDefStart_game():"Start guessing the size of the game" ' Current_money =Print (' You had ${} 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 (curren T_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 () /c11> 

After the command line, switch to the guess_exe.py directory,
Input:

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***.txt document in the build, detailing 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

Want to start the actual combat small white look over here, there is netease Cloud class best-selling Python course: Python combat: Around the Learning crawler system



Muggle programming
Links: https://www.jianshu.com/p/8dbdfbd3716d
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

Error:

Pyinstaller cannot check for assembly dependencies.

Please install PyWin32 or pywin32-ctypes.

The installation

Pip Install Pywin32-ctypes

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.