Python packaged into exe executable file

 Python program packaging tools

Python is a scripting language that is interpreted interpreter. Its publishing:

.py file: or less important source for open source projects, the source code directly, users need to install Python on their own and rely on installation of various libraries. (Python various official installation package is to do so).
.pyc file: Some companies or individuals as secret or a variety of reasons, do not want to see the source code to be run by, you can use pyc file publishing, pyc file is a Python interpreter can identify binary code, so the release is cross-platform, users need to install the appropriate version of Python and dependent libraries.
Executable: For non-agricultural user code or some white users, you let him put on a Python while also tossing a bunch of dependent libraries, it was a disaster. For such users, the easiest way is to provide an executable file, just need to tell him to use. The only problem was the need need to package different executable files for different platforms (Windows, Linux, Mac, ... ).
.py and .pyc are relatively simple, Python itself can handle. The Python script into an executable file has packaged a variety of ways.

PyInstaller

Installation pyinstaller

For those relatively stable network, the source address can be fluent pip user, directly following commands can get:

pip install pyinstaller

 

Usually we will download the source package, and then enter the package directory, execute the following command (you need to install setuptools):

python setup.py install

 

After installation, check successfully installed or not:

pyinstaller --version

 

After a successful installation, you can use the following commands:

pyinstaller: The main command packaged executable file, the following detailed usage will be introduced.
pyi-archive_viewer: View a list of executable files inside the package.
pyi-bindepend: View executable files rely on a dynamic library (.so or .dll file)
pyi- ...: and so on.

Use PyInstaller

        pyinstaller 的语法:pyinstaller [options] script [script…] | specfile

        The simplest usage, execute commands in the same directory and myscript.py:

         pyinstaller mycript.py

         Then see the new addition of two directories build and dist, dist following file is an executable file that can be published, for the above command you will find the dist directory under a pile of papers, all kinds of dynamic libraries are executable and myscrip file. Sometimes it feels too much trouble, you need to package all dist following things to post, in case of losing a dynamic library will not run, but fortunately pyInstaller support single file mode, only you need to do:

        pyinstaller -F mycript.py

        Here you will find dist only one executable file, this single file can be released, it can be run under a similar system operating system you are using. Of course, pyinstaller there are a variety of options, there are common options, such as the -d option is used to debug.

        In performing pyInstaller command, and will be in the same directory as the script, .spec generate a file that tells pyinstaller how to handle all of your script contains the command options. Generally, we do not have to ignore this file option if the need to package the data file, or to increase the packed binary to run some Python's ... when some advanced packaging options, .spec need to manually edit the file. can use:

        pyi-makespec optionsscript [script …]

        .Spec create a file, the file manually edited for .spec, we can use any one of the following commands:

        pyinstaller specfile

        pyi-build specfile

PyInstaller Introduction Principles

        PyInstaller in fact, the python parser and your own scripts packaged into an executable file, and compiled into machine code really is completely different, so do not expect to be packaged into a single executable file will improve operational efficiency, the opposite may will reduce the operating efficiency, the advantage is on the person running the machine without installing python script and your dependent libraries. In the Linux operating system, binutil tool it is mainly used inside the package ldd and objdump command.

        PyInstaller enter the script you specify, the first analysis of other scripts script relies on, and then to find, copy, to collect all relevant scripts together, including Python parser, and then put these files in a directory, or packaged into a executable file inside.

        The entire document can be published directly output inside the folder, or the resulting executable file. You only need to tell the user that your application App is self-contained and does not require installation of other packages, or a version of Python, you can run directly.

        Note that, PyInstaller packed executable file, and only in packaging machine systems under the same conditions. In other words, it does not have the portability, if you need to run on a different system, it must be packaged for the platform.

Practical issues

        exe run after pyinstaller pack how to remove the pop-up Command Prompt window?

        1. If using .spec documents, found in the file console = True modify the console = False

        2. If the file is specified directly pyinstaller python package, then you need to add -noconsole

        pyinstaller path\mycode.py–noconsole

        If you want to just pack a exe:

        pyinstaller -F path\mycode.py –noconsole

       or:

        pyinstaller -F -wpath\mycode.py

        Exe eventually replace the Path

        In cmd, outset cd D: \ PythonEXE \ folder switched to the output, and then with the above code, description: effect of various parameters,

        例子:pyinstaller -F -w -pD:\tmp\core-python\libs -i d:\tmp\main.ico main.py

        -F represents a single executable file generation;

        -D -onedir create a directory that contains exe file, but a lot will depend on the file (the default option).

        -w represent remove the console window, which is very useful when a GUI interface. But if it is a command line program, then it would put this option to delete it! ;

        -c -console, -nowindowed using the console, no interface (default);

        -p express your own custom class path to load, less than under normal circumstances;

        -i represents the icon of the executable file.

py2exe

         (Py2exe seems to only support python3.3 and pyhton3.4)

         py2exe is to convert a python script into an executable program can be executed independently on the windows (* .exe) tools, so you can not install python and run the executable program in the windows system. 

        py2exe has been used to create wxPython, Tkinter, Pmw, PyGTK, pygame, win32com client and server, and other stand-alone program. py2exe is released under an open source license.

py2exe usage 

        If you have a python script called myscript.py you want to convert it to run executable programs on windows and run on windows system is not installed python, then first of all you should write a program for publishing For example script provided mysetup.py, in front of which the setup function insert statement import py2exe. 

mysetup.py examples are as follows: 


# mysetup.py 

from distutils.core import setup 

import py2exe 

setup(console=["myscript.py"]) 

 

Then press the following method to run mysetup.py: 

python mysetup.py py2exe 

        The above command will generate a subdirectory named dist execution, which contains the myscript.exe, python24.dll, library.zip these files.

        If your myscript.py script using a compiled C extension modules, these modules will be copied in a subdirectory, the same, all the dll files at run time are required, in addition to the dll file system. dist subdirectory of the file containing your program must be something that you should be released with all this subdirectory.

        By default, py2exe must create the following files in the directory dist: 

        1, one or more exe files.

        2、python##.dll。

        3, several .pyd files, which are compiled extensions, which are exe files needed; plus other .dll files, these .dll is .pyd need. 

        4, a library.zip file that contains the compiled pure python module as .pyc .pyo above mysetup.py or create a myscript.exe a console program, if you want to create a graphical user interface program then you only need to mysetup.py the console = [ "myscript.py"] replace windows = [ "myscript.py"] either. 

        py2exe once to create multiple exe files, you need to pass the list of script files to the console or windows keyword arguments. If you have several scripts associated, then this is very useful. 

        Run the following command will display all the command-line flag py2exe command. 

        python mysetup.py py2exe–help 

Specify additional file 
       some applications require additional files at runtime, such as configuration files, fonts, bitmaps. If you install a script using those extra files data_files option is specified, then the py2exe can copy these files to dist subdirectory. data_files should contain a tuple (target-dir, files) list, which is a list of these files of additional documents. 

Examples are as follows: 


# mysetup.py

from distutils.core import setup

import glob

import py2exe 

 

setup(console=["myscript.py"], 

data_files=[("bitmaps", ["bm/large.gif","bm/small.gif"]), ("fonts", glob.glob("fonts\\*.fnt"))], ) 

 

Description: data_files option will create a subdirectory dist \ bitmaps, which contains two .gif files; a subdirectory dist \ fonts, which contains all the .fnt files.

 

Windows NTservices 
        you can pass keyword arguments to setup a service function to build Windows NT services, the value of the service parameter list must be a Python module name (including a service class). 

Examples are as follows: 


# mysetup.py

from distutils.core import setup

import py2exe

setup(service=["MyService"]) 

 

Executable service that is built by followed by a certain command line arguments from the mark-line installation and uninstallation.

Packing attached --pyinstaller parameters:

 

Guess you like

Origin www.cnblogs.com/valorchang/p/11357358.html