During the actual use of Pythonexe, many people may be puzzled by the actual operation steps. The following article describes the steps of Pythonexe in detail, the following is the detailed content of the article. If you are interested in it, you will find some benefits.
Procedure
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, go to the d: \ pythonlib directory, and run python-OO compileall. py-f. Compile the. py file in lib into the. pyo file.
- Introduction to common Python modules
- Procedure
- How to speed up Python string SEARCH
- Application Cases of Python map ease-of-use techniques
- How can I evaluate Python default parameters?
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>
- #include <stdlib.h>
- #include <stdio.h>
-
- 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 at runtime
- sprintf(szCmd, "PATH=%s\\dlls;%%PATH%%", szPath);
- _putenv(szCmd);
-
Set sys. path to ['.', 'Your source code zip 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_OptimizeFlag = 2;
- Py_NoSiteFlag = 1;
- Py_Initialize();
- PyRun_SimpleString(szCmd);
- return 0;
- }
The above describes the actual steps for manual Pythonexe executable programs.