|
| 1 | +# Python Core Code Encryption and EXE Packaging Guide |
| 2 | + |
| 3 | +This document records the entire process of compiling Python core algorithm code (`.py`) into low-level binary files (`.pyd`) to prevent source code leakage from decompilation, and finally packaging it into a portable single-file `.exe`. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Phase 1: Prepare C++ Compilation Environment (The Most Critical Step) |
| 8 | + |
| 9 | +Because Cython's encryption principle is to first translate Python code into C language and then compile it into machine code, Windows computers must install Microsoft's C++ compiler, otherwise it will report an error: `Microsoft Visual C++ 14.0 or greater is required`. |
| 10 | + |
| 11 | +1. **Download Official Tools**: |
| 12 | + Visit the official Microsoft download page to get **Microsoft C++ Build Tools**: |
| 13 | + 🔗 https://visualstudio.microsoft.com/visual-cpp-build-tools/ |
| 14 | +2. **Install Core Components**: |
| 15 | + Run the downloaded `vs_buildtools.exe`. |
| 16 | + In the installation interface that pops up, **be sure to check** the following in the top left corner: |
| 17 | + ✅ **Desktop development with C++** |
| 18 | + *(After checking, core components such as MSVC and Windows SDK will be automatically selected on the right, requiring about 6~8 GB of space)*. |
| 19 | +3. **Restart Terminal**: |
| 20 | + After installation is complete, you **must close and reopen the CMD/terminal window** to ensure the compiler's environment variables take effect. |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +## Phase 2: Encrypt and Compile Python Source Code to Binary (.pyd) |
| 25 | + |
| 26 | +1. **Install Required Python Libraries for Compilation**: |
| 27 | + Run the following command in the terminal: |
| 28 | + ```bash |
| 29 | + pip install cython setuptools |
| 30 | + ``` |
| 31 | + |
| 32 | +2. **Create Compilation Script**: |
| 33 | + Create a new file named `compile_algo.py` in the project root directory and fill in the following code: |
| 34 | + ```python |
| 35 | + from setuptools import setup |
| 36 | + from Cython.Build import cythonize |
| 37 | + |
| 38 | + # Place the core algorithm files that need encryption protection here |
| 39 | + compile_files = [ |
| 40 | + "filter_algo.py", |
| 41 | + "pose_algo.py" |
| 42 | + ] |
| 43 | + |
| 44 | + setup( |
| 45 | + ext_modules=cythonize(compile_files, compiler_directives={'language_level': "3"}) |
| 46 | + ) |
| 47 | + ``` |
| 48 | + |
| 49 | +3. **Execute Compilation Command**: |
| 50 | + Run in the terminal: |
| 51 | + ```bash |
| 52 | + python compile_algo.py build_ext --inplace |
| 53 | + ``` |
| 54 | + *After successful compilation, binary files like `filter_algo.cp310-win_amd64.pyd` will be generated in the project directory.* |
| 55 | + |
| 56 | +4. **⚠️ Extremely Important: Remove Original Source Code**: |
| 57 | + To prevent the plaintext source code from being packaged, **you must rename and backup the original `filter_algo.py` and `pose_algo.py`, or move them to another folder!** |
| 58 | + *(When running and packaging, Python will automatically recognize and call the generated `.pyd` black-box files without affecting functionality at all)*. |
| 59 | + |
| 60 | +--- |
| 61 | + |
| 62 | +## Phase 3: Package the Program as a Standalone EXE Executable |
| 63 | + |
| 64 | +After the core algorithms have been protected by binarization, we can package the main program (UI interface) into an EXE. |
| 65 | + |
| 66 | +1. **Install PyInstaller Packaging Tool**: |
| 67 | + ```bash |
| 68 | + pip install pyinstaller |
| 69 | + ``` |
| 70 | + |
| 71 | +2. **Execute Streamlined Packaging Command (Keep External Config File)**: |
| 72 | + To allow end-users to freely modify `config.json` and replace the Logo, we **should not** package these resources inside the EXE, but keep them in the same directory as the EXE. |
| 73 | + Run in the terminal: |
| 74 | + ```bash |
| 75 | + pyinstaller -F -w monitor_3d.py |
| 76 | + ``` |
| 77 | + **Parameter Explanation**: |
| 78 | + * `-F`: Package into a standalone single-file EXE. |
| 79 | + * `-w`: Windowed mode (hides the black CMD command line window behind it when running). |
| 80 | + |
| 81 | +3. **Get the Final Result**: |
| 82 | + After packaging is complete, you can find the final `monitor_3d.exe` in the generated `dist` folder in the project. |
| 83 | + |
| 84 | + **Final Release Structure**: Take `monitor_3d.exe` out of `dist`, place it in the same directory as the external `config.json` and `assets` folders, and compress them into a zip file to send to customers. This not only protects the core algorithms (sealed in the EXE) but also allows customers to customize the OEM interface. |
| 85 | + |
| 86 | +--- |
| 87 | +> **Summary Note**: |
| 88 | +> If you only modify the UI logic in `monitor_3d.py` in the future, you don't need to repeat Phase 1 and Phase 2. Just run the PyInstaller command in Phase 3 to repackage. |
| 89 | +> Only when you modify `pose_algo.py` or `filter_algo.py` do you need to rerun `python compile_algo.py build_ext --inplace` to regenerate the `.pyd` files. |
0 commit comments