Manually publish a python project as an exe executable program

Source: Internet
Author: User
This article mainly introduces how to manually publish a python project as an exe executable program. This article uses the C language to implement a simple Python package program. For more information, see section 1. manual preparation of the python exe executable program Python does not have an internal function to compile as 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. the methods described in this article are based on the following functions of python:

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 APIs for C-language programs to conveniently 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) first go to the c: \ python25 \ Lib directory and copy all the files, for example, to the d: \ pythonlib directory.
2) open a cmd window, go to the d: \ pythonlib directory, and run python-OO compileall. py-f. Compile the. py file in lib into the. pyo file.
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) put 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.

The code is as follows:


# Include
# Include
# Include
# Include

Int main ()
{
// Obtain 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 the PATH for running
Sprintf (szCmd, "PATH = % s \ dlls; % PATH %", szPath );
_ Putenv (szCmd );

// Set sys. path to ['.', 'Your source code zip file', 'standard Library zip file', 'dll directory']
// 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_OptimizeFlag = 2;
Py_NoSiteFlag = 1;
Py_Initialize ();
PyRun_SimpleString (szCmd );
Return 0;
}

9. put python25.dll in the d: \ dist directory.

Conclusion

In this case, there are only four files in the d: \ dist directory and one directory is added:
Dlls Directory: used to store all dll files and pyd files
Stdlib.zip File: used to store standard libraries in all python. pyo file formats
Mysrc.zip File: used to store programs you write. Note that the entry to the self-written program is in main. pyo.
Runpy.exe File: The startup file of the program. after the program is started, the sys. path of python is set and the main module is called.
Python25.dll File: The core dllof python. 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.

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.