Python Is a scripting language , Interpreted and executed by interpreter . The way it's released :

  • .py file : Not so important for open source projects or source code , Provide source code directly , It needs to be installed by the user Python And install the various libraries that depend on .(Python This is what the official installation packages do )

  • .pyc file : Some companies or individuals because of confidentiality or various reasons , I don't want the source code to be seen by the operator , have access to pyc Document release ,pyc File is Python The binary code that the interpreter can recognize , So it's also cross platform after it's released , The user is required to install the corresponding version of Python And dependency Libraries .

  • Executable file : For non code farmers or some small white users , You make him pretend to be Python At the same time, we have to make a lot of dependency libraries , It was a disaster . For such users , The easiest way is to provide an executable file , Just tell me the usage Ta that will do . The trouble is that different executable files need to be packaged for different platforms (Windows,Linux,Mac,...).

This article mainly introduces the last way ,.py and .pyc It's all pretty simple ,Python It can be done by itself . take Python There are many ways to package scripts into executable files , This article focuses on PyInstaller, Others are for comparison and reference only .

Freezing Your Code

A comparison of the various packaging tools is as follows ( From the article Freezing Your Code):

Solution Windows Linux OS X Python 3 License One-file mode Zipfile import Eggs pkg_resources support
bbFreeze yes yes yes no MIT no yes yes yes
py2exe yes no no yes MIT yes yes no no
pyInstaller yes yes yes no GPL yes no yes no
cx_Freeze yes yes yes yes PSF no yes yes no
py2app no no yes yes MIT no yes yes yes

PS. among pyInstaller and cx_Freeze It's all good ,stackoverflow It's also suggested on the Internet cx_Freeze, It's more convenient .pkg_resources New version of the pyInstaller It seems to be supportive .

install PyInstaller

For those networks, it's more stable , Able to use smoothly pip User with source address , Just follow the command below :

pip install pyinstaller

We usually download the source code package , Then go to the package directory , Execute the following command ( Need to install setuptools):

python install

After the installation , Check for successful installation :

pyinstaller --version

After successful installation , You can use the following command :

  • pyinstaller : The main command that packages the executable , Detailed usage is described below .
  • pyi-archive_viewer : View the list of files in the executable package .
  • pyi-bindepend : View the dynamic library on which the executable depends (.so or .dll file )
  • pyi-... : wait .

Use PyInstaller

pyinstaller The grammar of :

pyinstaller [options] script [script ...] | specfile

The simplest use , In the and Execute the command in the same directory :


Then you will see two new directories added build and dist,dist The following files are executable files that can be distributed , For the above commands you will find dist There are a bunch of files under the directory , Various dynamic library files and myscrip Executable file . Sometimes it feels like a hassle , Need to pack dist Everything below will be published , In case you lose a dynamic library, it won't work , Fortunately pyInstaller Support single file mode , Just execute :

pyinstaller -F

You'll find that dist There is only one executable file below , This single file is ready to be published , It can run under a similar system to the operating system you are using .

Of course ,pyinstaller There are also various options , There are general options , Such as -d The options are for debug, understand pyInstaller The process of execution ; There are also options for different platforms , The specific usage can be accessed PyInstaller official WIKI.

In execution pyInstaller When ordered , Will be in the same directory as the script , Generate a .spec file , The file will tell pyinstaller How to handle all your scripts , Also includes command options . Normally we don't bother with this file , If you need to package data files , Or add a little bit to the packed binary Python Runtime options ... Some advanced packaging options , You need to edit it manually .spec file . have access to :

pyi-makespec options script [script ...]

Create a .spec file , For manual editing .spec file , We can use any of the following commands :

pyinstaller specfile
pyi-build specfile

PyInstaller Brief introduction of the principle of

PyInstaller It's just a way of python The parser and your own scripts are packaged into an executable file , And compiling it into actual machine code , So don't count on packaging as an executable to improve performance , On the contrary, it may reduce operating efficiency , The advantage is that there is no installation on the runner's machine python And libraries that your script depends on . stay Linux Under the operating system , It mainly USES binutil In the kit ldd and objdump command .

PyInstaller Enter the script you specified , Start by analyzing the other scripts on which the script depends , And then go and look it up , Copy , Collect all the related scripts , Include Python Parser , Then put the files in a directory , Or package it in an executable file .

You can publish the output directly to an entire folder of files , Or the generated executable . You just have to tell the user , Your application App It's self-contained , No additional packages need to be installed , Or some version of it Python, You can just run it .

It should be noted that ,PyInstaller Packaged execution file , Only in the same environment as the packaging machine system . in other words , No portability , If you need to run on different systems , You have to package it for that platform .

Reference material

  1. Freezing Your Code
  2. PyInstaller official WIKI
  3. PyInstaller Source code

take Python More articles on how to package scripts into executable files

  1. take Python The script is packaged as an executable —— Reprint

    Python Is a scripting language , Interpreted and executed by interpreter . The way it's released : py file : Not so important for open source projects or source code , Provide source code directly , It needs to be installed by the user Python And install the various libraries that depend on .(Python Official installation ...

  2. PyInstaller hold Python Script packaged into executable program tutorial

    One . explain There's always been one .py The files are packed into .exe The idea of the document , But it's not strong enough , Every time I drag, I forget . I wrote a script for my colleagues in the hardware department yesterday , And then this afternoon , He asked if there was a way to package scripts into executable files , This is convenient to use ...

  3. Python The script is packaged as exe Executable files

    demand A teaching assistant directory structure checking tool , The directory structure is [ Books ]-[ chapter ]-[ subject | answer ]-[*.jpg], There is an asynchronous processing service in the background , You need to rely strongly on this directory structure . Book analysis is separate pipeline, Logs are not visible to users , here ...

  4. hold python The script is packaged as win Executable file

    A few days ago, a friend came to me to write something , After writing the code, I sent it to him and helped him do it for a long time , I didn't understand , I found this way . 1. Import pyinstaller package , pip install pyinstaller 2. Get into what you need to play ...

  5. hold Python The script is packaged to run by double clicking .exe file 【 turn 】

    Because it's going to be used recently Python Script , So I studied for a while , By the way, learn how to package it into .exe Executable file , Achieve the effect of double-click operation , I found information on the Internet , Save it for learning , The source of the original text is :https://baijiahao.b ...

  6. python The script is packaged as rpm software package

    Preface Software will eventually come in the form of delivery , Some use tar package , There is a directory , Some are sealed as a package , In most scenarios , It's the easiest way to package directly , It's also the least error prone , The path can be written dead in the package practice About packaging ...

  7. hold python Package files into executable files (win10 Successful experiment )

    There's always someone coming to me to check the work order status , I'm too lazy to show it on the page , Just pack it up exe The papers are ready Packing is easy , The difficulty is installation pyinstaller This depends on the package , The main problem is the network ~ I also refer to other people's blogs , Other people's articles are very detailed , I ...

  8. python The script is packaged as exe Executable file

    1. Tools pyinstaller (pip install pyinstaller) 2. Generate exe (pyinstaller -F -w Parameters -F Generate a single text ...

  9. 【 Reprint 】 take python The script is packaged as exe file

    exe Files are files that can be executed directly . Usually the tapes we weave py Suffix script files need to have python Under the environment of That's ok , Each time through Win + R Open the run window and enter powershell Open console , After all the hard work cd ...

Random recommendation

  1. Knockout.Js Learn from the official website (value binding )

    Preface value Binding is association DOM The value of the element goes to view model On the properties of . Mainly used in form control <input>,<select> and <textarea> On . When users edit the form ...

  2. MetaWeblog Blog client tools Windows Live Writer

    Make complaints & Attention pit : I just heard that there is such a thing , It is said that Windows Live Writer After open source, it was renamed Open Live Writer, I thought Open Live Writer It's better than Windows ...

  3. SQLServer User name configuration of login name in

    Actually, this problem has been bothering me for a long time . This summer (13.7) First contact during internship sqlserver At that time, it was a unified arrangement , Just do it step by step . It was used to link databases at that time id yes sa. Later, when I made a small program, I used it no matter what sa Log in well ...

  4. 319. Bulb Switche

    There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...

  5. hdu1017 #include<iostream> #include<stdio.h> #inc ...

  6. Spring in action note

    Two sides of coupling     On the one hand, code coupling is difficult to test , It's going to be like a hamster bug characteristic ( Fix one bug, Trigger another bug) On the other hand, coupling is necessary , Different classes must interact appropriately , In order to achieve the function . bean Four kinds of packing formula ...

  7. PHP Attention should be paid to Alipay's immediate arrival.

    Be careful :1 According to other people's parameter rules , Standard fill in parameter list :2 The business information is filled in correctly :3 Then after submitting, pay attention to say goodbye to our server at this time , The order information on our server will be submitted to the Alipay server. , Then the Alipay server processes the Alipay payment process. , When paying ...

  8. Servlet and JSP Reading notes ( Two )

    One . GenericServlet 1.  It was written before Servlet and JSP Learning notes ( One ) All of the examples in are implemented through Servlet Interface written , The problem is that : You have to do it Servlet All the methods in , Regardless of ...

  9. mysql The remote access , modify root password

    mysql -uroot -p #input password use mysql; update user set host='%' where user='root'; flush privile ...

  10. Under Alibaba cloud Linux Server installation Redis

    What is? Redis? Redis Is a persistent caching framework , Support distributed cache , Simple and easy to use . Similar frameworks include memcached, It's a Key-Value Cache framework of formal storage .   The frameworks that can be used as caching are : EHCa ...