問題描述
使用另一個 Python 文件的主 Python 文件(我想轉換為 .exe) (Main Python File Using Another Python File (I Want To Convert To .exe))
我創建了一個包含 2 個 .py 文件
的程序。我想讓程序成為一個 .exe
文件,我使用 cx_Freeze
來實現。我的問題是我將 main.py
轉換為 .exe
但第二個 Python 文件仍然是 .py
文件。我不希望它是 .py
因為如果它是用戶可以看到代碼。如果我還轉換了第二個 Python 文件,則程序無法運行,因為我在主文件中 import
了 Python 文件。有什麼建議麼?(我不想將第二個 Python 文件複製到主 Python 文件)
參考解法
方法 1:
You are chasing the wrong rabbit here. The various tools that generate executable files from Python code are not compilers. They are just tools that embed a Python interpretor with py (or pyc) files to allow users to use the program without a prior Python installation.
Said differently you should not use them to hide your code (except from people not knowing a lot of Python): a pyc does not contain text code but according to the answers to Is it possible to decompile a compiled .pyc file into a .py file? , tools exists that convert back a pyc file into a py file (of course except the comments).
IMHO, you should better google for python obfuscate to find tools dedicated to obfuscation, what neither cx‑freeze
nor pyinstaller
are.
BTW while there are appropriate use cases for obfuscation you should be aware that a determinate attacker can always circumvent it. The reason why the real protection for intellectual property is law and not technics...
方法 2:
I'm not sure how two or more .py
files can be converted to .exe
.
But in python the easiest way to convert to .exe is a module named pyinstaller .
You can install it using command pip install pyinstaller
can get it . After just go to the directory where your project files are and open command prompt in that directory and execute pyinstaller file_name
(by user14674970、Serge Ballesta、Saffron‑codes)