Manually create the python EXE executable program

Source: Internet
Author: User
1. Manually make the python EXE executable Program

Reprint ---------------

Python does not have the function of compiling to exe. It brings a lot of trouble to the deployment of Python programs.

A few tools similar to py2exeare displayed, which is used to compile the. pyfile into the. exe file automatically.

Recently, I have made some research on the manual Implementation of functions similar to py2exe, hoping to enhance my understanding of Python.

The results are quite good. Record the results and share them with you.

 

1.1. Principles

The methods described in this article are based on the following Python functions:

1. When the python program is running, the library file will be searched in the path specified by sys. Path.

2. Python starts from 2.3 and supports importing libraries from ZIP files (. py,. PyC and. Pyo are supported, but. PYD is not supported)

3. Python provides c apiC LanguageYou can easily call Python programs.

 

1.2. Actual steps

Note: Assume that python is installed in the C:/python25 directory, and the final executable file is placed in the D:/Dist directory.

1. Go to the C:/python25/lib directory and copy all the files, for example, to the D:/pythonlib directory.

2. Open a cmd window, enter the D:/pythonlib directory, and runPython-oo compileall. py-f.Compile all. py files in lib into. Pyo files.

3. Delete all. py and. PyC files in the D:/pythonlib directory, because we can run these libraries as long as there is a. Pyo file.

4. Delete all unnecessary files in the directory, such as curses, test, idlelib, and msilib, to reduce the volume of generated files.

5. Pack these databases into a zip file, such as stdlib.zip, and put them in the D:/Dist directory.

6. in the C:/python25/DLLs directory. PYD and. DLL file, copied to the D:/Dist/DLLs directory, of course, delete some files that are not available _ MSI. PYD, _ SSL. PYD and so on can reduce the file volume

7. Compile the program written in step 2 to Step 5 into a mysrc.zip package and put it in the D:/Dist directory.Note: the entry to the self-written program should be the main. Pyo file.

8. Compile an executable file using the C program, and call runpy.exe, which is also stored in D:/Dist.

 

# Include <python. h> # include <windows. h> int main () {// get the directory of the current Executable File char szpath [10240]; char szcmd [10240]; getmodulefilename (null, szpath, sizeof (szpath); char * P = strrchr (szpath, '//'); If (P = NULL) {printf ("Get module File Name error! /N "); Return-1;} * p = 0; // set SYS. Path to ['.', 'your ownSource codeZIP file ', 'standard library zip file', 'dll directory'] // then call the main module sprintf (szcmd, "Import sys/N" "sys. path = ['. ', R' % S // mysrc.zip', r '% S // stdlib.zip', r '% S // dlls']/n "" import main/N ", szpath, szpath, szpath); py_initialize (); pyrun_simplestring (szcmd); Return 0 ;}

9. Put python25.dll in the d:/Dist directory.

 

1.3. Conclusion

In this case, in the D:/Dist directory, there are only four files added with a directory:

    • DLLs directory: Used to store all DLL files and PYD files

    • Stdlib.zip File: A standard library used to store all python. Pyo file formats

    • Mysrc.zip File: Used to store self-written programs. Note that the entry to the self-written program is in Main. Pyo.

    • Runpy.exe File: The Startup file of the program. After it is started, the python SYS. path will be set, and then the main module will be called.

    • Python25.dll File: The core of pythondll, runpy.exe depends on this DLL

 

 

    • Haha, it's quite concise and clear. A total of four files are in one directory, which is less than 5 MB.

Note: of course, this packaging method is troublesome for the first time, but you can just package your program later, and you don't need to change anything else.

In addition, if your program is frequently modified, you can leave it unpackaged and directly put it in D:/dist. when runpy.exe starts the program, you only need to run import main normally.

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.