Manual implementation of the Python project published as an EXE executable process sharing _python

Source: Internet
Author: User
Tags sprintf

1. Hand-crafted Python exe executables Python does not have a built-in feature compiled as EXE. A lot of trouble with the deployment of Python programs. So there will be some py2exe and other good tools for automatically compiling. py files into. exe files.

Recently took the time to study the manual implementation of similar Py2exe functions, I hope to enhance the understanding of Python. The results are quite good. Record the results and share them with you.

1.1. The method described in the principle is based on the following functions of Python

1 when the Python program runs, it looks for the library file in the path specified by Sys.path.
2 Python starts from 2.3, supports import library from zip file (support. Py,.pyc and. Pyo, but not support. PYD)
3 Python provides C API, so that the C language program, can easily invoke the Python program

1.2. Practical steps Note: Assuming that Python is installed in the C:\python25 directory, the final executable file is placed in the D:\dist directory

1 First go to the C:\python25\Lib directory, copy all the files, such as copy to the D:\pythonlib directory
2 Open a CMD window, into the D:\pythonlib directory, running Python-oo compileall.py-f. Compile the. py file in Lib into a. pyo file
3 Delete all the. py and. pyc files in the D:\pythonlib directory, because we can have the. pyo file to run the libraries.
4 Delete all unnecessary files in the directory, such as Curses,test,idlelib,msilib, to reduce the volume of the generated files.
5 package These libraries into a zip file, such as Stdlib.zip, and put them in the D:\dist directory.
6 The C:\python25\dlls directory of the. PYD and. dll files, copied to the D:\dist\dlls directory, of course, delete the impossible to use some of the files _msi.pyd,_ssl.pyd, etc., you can reduce the volume of the file
7 Write your own program, also in steps 2 to 5 of the method described, into a mysrc.zip package, put into the D:\dist directory. Note: The entry of your own program should be the Main.pyo file
8 The following C program to compile an executable file, such as Runpy.exe, also put in the d:\dist.

Copy Code code as follows:

#include <Python.h>
#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>

int main ()
{
To get the directory where the current executable file resides
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 at run time
sprintf (Szcmd, "path=%s\\dlls;%%path%%", szpath);
_putenv (Szcmd);

Set Sys.path to ['. ', ' own source code zip file ', ' Standard library zip file ', ' dll directory ']
And 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_optimizeflag = 2;
Py_nositeflag = 1;
Py_initialize ();
Pyrun_simplestring (Szcmd);
return 0;
}

9. Place the Python25.dll in the D:\dist directory.

Conclusion

In this way, there are only 4 files plus a directory in the D:\dist directory:
DLLs directory: for storing all DLL and PYD files
Stdlib.zip file: A standard library for storing all the. pyo file formats for Python
Mysrc.zip file: For storing your own written program. Note that the entry of your own written program is in Main.pyo.
Runpy.exe file: The startup file for the program, which will set up Python's Sys.path and call the main module
Python25.dll file: Python's core Dll,runpy.exe relies on this DLL

--------------------------------------------------------------------------------

Haha, quite concise and clear. A total of only 4 files a directory, 5MB not to OH.
Note: Of course, this packaging method for the first time to do more trouble, but then you can just pack your own program, the other do not change.
Moreover, if their program often make changes, their own program can not be packaged, directly into the d:\dist, anyway, runpy.exe start the program, as long as the normal operation of import main on it.

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.